[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

extract a range start/end?

Michael Linfield

9/2/2007 8:22:00 PM

if i have a list of dates in an array such as:

4/2/07
4/3/07
4/4/07
4/5/07
4/6/07
4/7/07
4/8/07
ect.

how would i..based on option parse..pull a starting point and end point
of to extract that data range.

IE:

opts = OptionParser.new do |opts|
opts.on("-s", "--startdate", "What start date to use." do |i|
#code to determine to use the starting date
end

# the same would apply to an --enddate

any ideas?

i was thinking i could possibly create a new range depending on what the
user input was by doing

require 'date'
results = []

(Date.new(2007,4,1)..Date.new(2007,4,8)).each {|r| res << r}
but then how would i match that against the original dates for the data?

feel free to give me any thoughts.

-Thanks
--
Posted via http://www.ruby-....

8 Answers

Wilson Bilkovich

9/2/2007 8:52:00 PM

0

On 9/2/07, Marcin Grzywaczewski <killavus@gmail.com> wrote:
> 2007/9/2, Michael Linfield <globyy3000@hotmail.com>:
> >
> > if i have a list of dates in an array such as:
> >
> > 4/2/07
> > 4/3/07
> > 4/4/07
> > 4/5/07
> > 4/6/07
> > 4/7/07
> > 4/8/07
> > ect.
> >
> > how would i..based on option parse..pull a starting point and end point
> > of to extract that data range.
> >
> > IE:
> >
> > opts = OptionParser.new do |opts|
> > opts.on("-s", "--startdate", "What start date to use." do |i|
> > #code to determine to use the starting date
> > end
> >
> > # the same would apply to an --enddate
> >
> > any ideas?
> >
> > i was thinking i could possibly create a new range depending on what the
> > user input was by doing
> >
> > require 'date'
> > results = []
> >
> > (Date.new(2007,4,1)..Date.new(2007,4,8)).each {|r| res << r}
> > but then how would i match that against the original dates for the data?
> >
> > feel free to give me any thoughts.
> >
> > -Thanks
> > --
> > Posted via http://www.ruby-....
>
>
> I think that range operates on numbers, and method "to_i" casts class to
> number, so your class should have specific method "to_i".
>

From rdoc:
Ranges can be constructed using objects of any type, as long as
the objects can be compared using their <=> operator and they
support the succ method to return the next object in sequence.

In practice, that usually means 'Anything that has Enumerable as an ancestor'

Florian Aßmann

9/2/2007 9:26:00 PM

0

Hi Michael,
> opts = OptionParser.new do |opts|
> opts.on("-s", "--startdate", "What start date to use." do |i|
> #code to determine to use the starting date

# see Parsedate#parsedate at
http://www.ruby-doc.org/stdlib/libdoc/parsedate/rdoc/classes/ParseDate.ht...

@startdate = Parsedate.parsedate i

> end

> i was thinking i could possibly create a new range depending on what the
> user input was by doing
>
> require 'date'
> results = []
# delete this

>
> (Date.new(2007,4,1)..Date.new(2007,4,8)).each {|r| res << r}
result = (@startdate..@enddate).to_a

> but then how would i match that against the original dates for the data?
match = original & result

Oyasumi
Florian

Florian Aßmann

9/2/2007 9:38:00 PM

0

little flaw:

Florian Aßmann schrieb:
> Hi Michael,
>> opts = OptionParser.new do |opts|
>> opts.on("-s", "--startdate", "What start date to use." do |i|
>> #code to determine to use the starting date
>
# see Parsedate#parsedate at
#
http://www.ruby-doc.org/stdlib/libdoc/parsedate/rdoc/classes/ParseDate.ht...
>
- @startdate = Parsedate.parsedate i
+ @startdate = Date.new(*ParseDate.parsedate(i)[0,3])

>
>> end



Michael Linfield

9/2/2007 10:28:00 PM

0

Florian AÃ?mann wrote:
> little flaw:
>
> Florian AÃ?mann schrieb:
>> Hi Michael,
>>> opts = OptionParser.new do |opts|
>>> opts.on("-s", "--startdate", "What start date to use." do |i|
>>> #code to determine to use the starting date
>>
> # see Parsedate#parsedate at
> #
> http://www.ruby-doc.org/stdlib/libdoc/parsedate/rdoc/classes/ParseDate.ht...
>>
> - @startdate = Parsedate.parsedate i
> + @startdate = Date.new(*ParseDate.parsedate(i)[0,3])

if you dont mind my asking,

@startdate = Date.new(*ParseDate.parsedate(i)[0,3])

can someone explain how that line of code works, i'd rather understand
it so that i actually know what it does when i insert it into my code

Thanks!
--
Posted via http://www.ruby-....

Joel VanderWerf

9/2/2007 11:14:00 PM

0

Wilson Bilkovich wrote:
>>From rdoc:
> Ranges can be constructed using objects of any type, as long as
> the objects can be compared using their <=> operator and they
> support the succ method to return the next object in sequence.
>
> In practice, that usually means 'Anything that has Enumerable as an ancestor'

You meant Comparable, right? There's no connection between Enumerable
and #<=> or #succ.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Florian Aßmann

9/2/2007 11:49:00 PM

0

Ok, I though you were familiar with some of the RubyDoc ressources...

Here I go:
ParseDate and Date are part of the Ruby StdLib, therefore in order to use this
you need to:
require 'parsedate'
require 'date'

Next thing I think you expect something like '1/20/2007' as dateformat:
/my_app.rb --startdate 1/20/2007

This 'll be parsed by:
ParseDate.parsedate('1/20/2007') # => [2007,1,20,nil,nil,nil]

To generate an instance of Date out of this we simply call Date.new, but wait,
Date.new only accepts 3 args (maybe 4, though).

Therefore I invoke the slice-method ([]) on the freshly baken array:
# I assume i = '1/20/2007'
ParseDate.parsedate(i)[0, 3] # => [2007,1,20]

To split the array into seperate arguments when I invoke Date.new I need the
*-Operator: a_method(*[1,2,3]) is like a_method(1,2,3)

Date.new(*ParseDate.parsedate(i)[0, 3]) # => <Date... >

Tada, you got your Date now...

Further reading:
Ruby Stdlib: Date and ParseDate
Ruby Classes and Libraries: Array.slice

Regards
Florian

Wilson Bilkovich

9/3/2007

0

On 9/2/07, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:
> Wilson Bilkovich wrote:
> >>From rdoc:
> > Ranges can be constructed using objects of any type, as long as
> > the objects can be compared using their <=> operator and they
> > support the succ method to return the next object in sequence.
> >
> > In practice, that usually means 'Anything that has Enumerable as an ancestor'
>
> You meant Comparable, right? There's no connection between Enumerable
> and #<=> or #succ.

You are correct. Not enough sleep this weekend.

Michael Linfield

9/3/2007 12:41:00 AM

0

> but then how would i match that against the original dates for the data?
> match = original & result

now as far as matching that date to grep'd data should i use a string
comparison or is there a better way?
--
Posted via http://www.ruby-....