Project

General

Profile

Actions

Bug #11621

closed

Date.- and Date.+ methods inconsistencies

Added by andreionut (Andrei Balcanasu) over 8 years ago. Updated over 8 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
[ruby-core:71192]

Description

I noticed today some inconsistencies when using - and + methods of the Date class, that makes the whitespace relevant:

irb(main):001:0> require 'date'
=> true
irb(main):002:0> Date.today - 7
=> #<Date: 2015-10-19 ((2457315j,0s,0n),+0s,2299161j)>
irb(main):003:0> Date.today -7
=> #<Date: 2015-10-26 ((2457322j,0s,0n),+0s,2299161j)>
irb(main):004:0> Date.today-7
=> #<Date: 2015-10-19 ((2457315j,0s,0n),+0s,2299161j)>

It looks like the parser ignores the -7 in the context of Date.today instead of interpreting it as 7 days ago

This can be replicated when using integers as variables:

irb(main):002:0> days = 5
=> 5
irb(main):003:0> p (Date.today - days) == (Date.today -days)
false
=> false

or when creating Date with DateTime.now, but weirdly enough not when the Date is in a variable:

irb(main):004:0> now = Date.today
=> #<Date: 2015-10-26 ((2457322j,0s,0n),+0s,2299161j)>
irb(main):005:0> p (now - 7) == (now-7)
true
=> true
irb(main):006:0> p (now -7) == (now-7)
true
=> true

or when creating a Date with the constructors that force you to set a date (new, ordinal, parse, etc)

The other thing that I discovered while testing this was the behaviour to_date when tried to chain the - method:

irb(main):002:0> Time.new(2001,2,3).to_date
=> #<Date: 2001-02-03 ((2451944j,0s,0n),+0s,2299161j)>
irb(main):003:0> Time.new(2001,2,3).to_date -7
ArgumentError: wrong number of arguments (1 for 0)
	from (irb):3:in `to_date'
	from (irb):3
	from /opt/boxen/rbenv/versions/2.2.3/bin/irb:11:in `<main>'
irb(main):004:0> Time.new(2001,2,3).to_date - 7
=> #<Date: 2001-01-27 ((2451937j,0s,0n),+0s,2299161j)>

I've attached a file with my tests and commented the output from my machine.


Files

date.rb (1.41 KB) date.rb andreionut (Andrei Balcanasu), 10/26/2015 03:54 PM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0