extract a range start/end?

  • Thread starter Michael Linfield
  • Start date
M

Michael Linfield

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
 
W

Wilson Bilkovich

2007/9/2 said:
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


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'
 
F

Florian Aßmann

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
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
 
M

Michael Linfield

Florian said:
little flaw:
Hi Michael,
# 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!
 
J

Joel VanderWerf

Wilson said:
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.
 
F

Florian Aßmann

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
 
M

Michael Linfield

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top