[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Finding last date

Dipesh Batheja

4/6/2007 9:01:00 AM

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

7 Answers

Robert Klemme

4/6/2007 10:05:00 AM

0

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

Jeremy Hinegardner

4/6/2007 12:46:00 PM

0

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/Nu...

enjoy,

-jeremy

--
========================================================================
Jeremy Hinegardner jeremy@hinegardner.org


Dipesh Batheja

4/6/2007 5:17:00 PM

0

Cool, thanks, thats great info.

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

srinsriram

4/6/2007 5:21:00 PM

0

On Apr 6, 6:05 am, Robert Klemme <shortcut...@googlemail.com> 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"

Sam Smoot

4/7/2007 2:34:00 AM

0

On Apr 6, 12:21 pm, srinsri...@gmail.com 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. :-)

Ken Bloom

4/8/2007 2:09:00 AM

0

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

> On Apr 6, 6:05 am, Robert Klemme <shortcut...@googlemail.com> 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...

Bertram Scharpf

4/8/2007 12:46:00 PM

0

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/Nu...

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