Extract a range start/end?

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

On 9/2/07, Marcin G. [email protected] wrote:

4/8/07
end

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’

2007/9/2, Michael L. [email protected]:

ect.

(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-forum.com/.

I think that range operates on numbers, and method “to_i” casts class to
number, so your class should have specific method “to_i”.

Yours,
Killavus.

2007/9/2, Wilson B. [email protected]:

4/6/07
opts = OptionParser.new do |opts|
user input was by doing
-Thanks
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’

Oh, my bad. Sorry :)…

Yours,
Killavus.

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.html#M001494

@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

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.html#M001494

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

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.html#M001494

  • @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!

Wilson B. 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.

On 9/2/07, Joel VanderWerf [email protected] wrote:

Wilson B. 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.

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?

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs