Finding last date

Discussion in 'Ruby' started by Dipesh Batheja, Apr 6, 2007.

  1. Hi,
    Is there any instance method in Ruby for Date type, to find previous
    date. For example if I have date - "04/05/07" in an object "date", then
    something like "date.previous" returns me date - "04/04/07".

    --
    Posted via http://www.ruby-forum.com/.
    Dipesh Batheja, Apr 6, 2007
    #1
    1. Advertising

  2. On 06.04.2007 11:00, Dipesh Batheja wrote:
    > Hi,
    > Is there any instance method in Ruby for Date type, to find previous
    > date. For example if I have date - "04/05/07" in an object "date", then
    > something like "date.previous" returns me date - "04/04/07".
    >

    $ irb -r date
    irb(main):002:0> Date.today.to_s
    => "2007-04-06"
    irb(main):003:0> (Date.today - 1).to_s
    => "2007-04-05"
    irb(main):004:0>

    robert
    Robert Klemme, Apr 6, 2007
    #2
    1. Advertising

  3. On Fri, Apr 06, 2007 at 06:00:50PM +0900, Dipesh Batheja wrote:
    > Hi,
    > Is there any instance method in Ruby for Date type, to find previous
    > date. For example if I have date - "04/05/07" in an object "date", then
    > something like "date.previous" returns me date - "04/04/07".


    The Date object can inherently do date arithmetic:

    irb(main):001:0> require 'date'
    irb(main):002:0> today = Date.today
    irb(main):003:0> yesterday = today - 1
    irb(main):004:0> puts "Today : #{today}"
    Today : 2007-04-06
    irb(main):006:0> puts "Yesterday : #{yesterday}"
    Yesterday : 2007-04-05

    So if you would like a today.prev then you can easily add #prev.
    Date#succ already exists.

    class Date
    def prev
    self - 1
    end
    end

    If you want even more interesting time/date utility functions add in the
    facets gem and try out the 'more/times' items, you can do things like
    1.week.ago and 3.hours.from_now.

    http://facets.rubyforge.org/src/doc/rdoc/more/classes/Numeric.html

    enjoy,

    -jeremy

    --
    ========================================================================
    Jeremy Hinegardner
    Jeremy Hinegardner, Apr 6, 2007
    #3
  4. Dipesh Batheja, Apr 6, 2007
    #4
  5. Dipesh Batheja

    Guest

    On Apr 6, 6:05 am, Robert Klemme <> wrote:
    > On 06.04.2007 11:00, Dipesh Batheja wrote:> Hi,
    > > Is there any instance method in Ruby for Date type, to find previous
    > > date. For example if I have date - "04/05/07" in an object "date", then
    > > something like "date.previous" returns me date - "04/04/07".

    >
    > $ irb -r date
    > irb(main):002:0> Date.today.to_s
    > => "2007-04-06"
    > irb(main):003:0> (Date.today - 1).to_s
    > => "2007-04-05"
    > irb(main):004:0>
    >
    > robert


    I just noticed that whitespace near the -1 makes a difference in the
    evaluation in irb. is this expected?

    irb(main):006:0> (Date.today -1).to_s
    => "2007-04-06"

    irb(main):008:0> (Date.today - 1).to_s
    => "2007-04-05"
    , Apr 6, 2007
    #5
  6. Dipesh Batheja

    Sam Smoot Guest

    On Apr 6, 12:21 pm, wrote:
    > I just noticed that whitespace near the -1 makes a difference in the
    > evaluation in irb. is this expected?
    >
    > irb(main):006:0> (Date.today -1).to_s
    > => "2007-04-06"
    >
    > irb(main):008:0> (Date.today - 1).to_s
    > => "2007-04-05"


    Yes. "-1" is "negative one", "- 1" is "minus one". So the former is
    interpreted as passing the value -1 to the method
    Date::today(sg=ITALY), while the latter is interpreted as calling
    Date#- on the instance returned from Date::today and passing 1 as the
    parameter to Date#-.

    So it just goes to show, spacing matters, so better to be clear than
    invalid.

    If you think about it, if the left-hand-side contains a ".", so a
    method call, the language parser can't interpret what you mean in this
    example without giving significance to the whitespace.

    If you try the same without a trailing method on the LHS, for example
    "1 -1", then just like in math, the meaning becomes unambiguos, since
    without interpreting the minus symbol as a call to Fixnum#-, there is
    no expression, and the line would otherwise just be a syntax error.

    So the parser is forgiving when it can be I suppose. Me, I prefer to
    just use whitespace always unless I mean to actually represent a
    negative number. To do otherwise just feels sloppy to me. :)
    Sam Smoot, Apr 7, 2007
    #6
  7. Dipesh Batheja

    Ken Bloom Guest

    On Fri, 06 Apr 2007 10:21:01 -0700, srinsriram wrote:

    > On Apr 6, 6:05 am, Robert Klemme <> wrote:
    >> On 06.04.2007 11:00, Dipesh Batheja wrote:> Hi,
    >> > Is there any instance method in Ruby for Date type, to find previous
    >> > date. For example if I have date - "04/05/07" in an object "date",
    >> > then something like "date.previous" returns me date - "04/04/07".

    >>
    >> $ irb -r date
    >> irb(main):002:0> Date.today.to_s
    >> => "2007-04-06"
    >> irb(main):003:0> (Date.today - 1).to_s => "2007-04-05"
    >> irb(main):004:0>
    >>
    >> robert

    >
    > I just noticed that whitespace near the -1 makes a difference in the
    > evaluation in irb. is this expected?
    >
    > irb(main):006:0> (Date.today -1).to_s => "2007-04-06"
    >
    > irb(main):008:0> (Date.today - 1).to_s => "2007-04-05"


    Yeah. The first way passes the value -1 as a parameter to Date.today.
    The second way does math.

    Ruby has lots of ambiguous operators like that (<< versus <<HEREDOC for
    example), your best strategy is to always put whitespace around your
    binary operators.

    --Ken

    --
    Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
    Department of Computer Science. Illinois Institute of Technology.
    http://www.iit.edu/~kbloom1/
    Ken Bloom, Apr 8, 2007
    #7
  8. Hi,

    Am Freitag, 06. Apr 2007, 21:45:43 +0900 schrieb Jeremy Hinegardner:
    > On Fri, Apr 06, 2007 at 06:00:50PM +0900, Dipesh Batheja wrote:
    > If you want even more interesting time/date utility functions add in the
    > facets gem and try out the 'more/times' items, you can do things like
    > 1.week.ago and 3.hours.from_now.
    >
    > http://facets.rubyforge.org/src/doc/rdoc/more/classes/Numeric.html


    Very nice. But unfortunately a month is not alway 30 days
    and a year not always 365.

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
    Bertram Scharpf, Apr 8, 2007
    #8
    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. Matt
    Replies:
    1
    Views:
    570
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,193
    Michael Borgwardt
    May 30, 2004
  3. Replies:
    10
    Views:
    890
    Dale King
    May 15, 2006
  4. Roedy Green

    Date last Accessed vs Date Modified

    Roedy Green, Feb 21, 2008, in forum: Java
    Replies:
    1
    Views:
    1,439
    Nigel Wade
    Feb 22, 2008
  5. Edward
    Replies:
    3
    Views:
    169
    Gunnar Hjalmarsson
    Sep 8, 2004
Loading...

Share This Page