extract a range start/end?

Discussion in 'Ruby' started by Michael Linfield, Sep 2, 2007.

  1. 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-forum.com/.
    Michael Linfield, Sep 2, 2007
    #1
    1. Advertising

  2. On 9/2/07, Marcin Grzywaczewski <> wrote:
    > 2007/9/2, 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
    > > --
    > > 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".
    >


    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'
    Wilson Bilkovich, Sep 2, 2007
    #2
    1. Advertising

  3. 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
    Florian Aßmann, Sep 2, 2007
    #3
  4. little flaw:

    Florian A=C3=9Fmann schrieb:
    > Hi Michael,
    >> opts =3D OptionParser.new do |opts|
    >> opts.on("-s", "--startdate", "What start date to use." do |i|
    >> #code to determine to use the starting date

    >=20

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

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

    >=20
    >> end
    Florian Aßmann, Sep 2, 2007
    #4
  5. 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!
    --
    Posted via http://www.ruby-forum.com/.
    Michael Linfield, Sep 2, 2007
    #5
  6. 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
    Joel VanderWerf, Sep 3, 2007
    #6
  7. 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
    Florian Aßmann, Sep 3, 2007
    #7
  8. On 9/2/07, Joel VanderWerf <> 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.
    Wilson Bilkovich, Sep 3, 2007
    #8
  9. > 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-forum.com/.
    Michael Linfield, Sep 3, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Noam Raphael
    Replies:
    17
    Views:
    508
    Noam Raphael
    May 26, 2004
  2. Richard Sandoval

    Extract a range i.e. svr[100..130] ?

    Richard Sandoval, Apr 15, 2011, in forum: Ruby
    Replies:
    12
    Views:
    216
    7stud --
    Apr 18, 2011
  3. it_says_BALLS_on_your forehead

    extract range of lines using range op bug?

    it_says_BALLS_on_your forehead, Mar 3, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    179
    it_says_BALLS_on_your forehead
    Mar 3, 2006
  4. Guest
    Replies:
    4
    Views:
    285
    Guest
    May 12, 2006
  5. Replies:
    2
    Views:
    289
Loading...

Share This Page