Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Effect of leaving space after '-' in Ruby expression

Today I was trying something out in my Rails console and this happened,

2.0.0p247 :009 > Date.today -29.days
 => Fri, 07 Feb 2014 
2.0.0p247 :010 > Date.today - 29.days
 => Thu, 09 Jan 2014 

I am pretty baffled. I can see that I am missing something basic. But it just inst striking my mind! Could anyone explain why this is happening?

like image 772
Steve Robinson Avatar asked Feb 07 '14 13:02

Steve Robinson


1 Answers

What actually happens is this:

Date.today(-29.days) # => Fri, 07 Feb 2014

today has an optional parameter called start, which defaults to Date::ITALY.

An optional argument the day of calendar reform (start) as a Julian day number, which should be 2298874 to 2426355 or -/+oo. The default value is Date::ITALY (2299161=1582-10-15).

Passing -29.days to today apparently has no effect.

Whereas:

Date.today + -29.days # => Thu, 09 Jan 2014

Which is the same as:

Date.today - 29.days # => Thu, 09 Jan 2014
like image 57
Mischa Avatar answered Oct 06 '22 00:10

Mischa