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”.
On 06.04.2007 11:00, Dipesh B. 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
On Fri, Apr 06, 2007 at 06:00:50PM +0900, Dipesh B. 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
Cool, thanks, thats great info.
On Apr 6, 6:05 am, Robert K. [email protected] wrote:
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”
On Apr 6, 12:21 pm, [email protected] 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.
On Fri, 06 Apr 2007 10:21:01 -0700, srinsriram wrote:
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
Hi,
Am Freitag, 06. Apr 2007, 21:45:43 +0900 schrieb Jeremy H.:
On Fri, Apr 06, 2007 at 06:00:50PM +0900, Dipesh B. 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