Issue #11621 has been updated by Shugo Maeda.

Status changed from Open to Rejected

Andrei Balcanasu wrote:
> Good point, but this still does not explain why in some cases ` -7` works:
> 
> ```ruby
> now = Date.today
> => #<Date: 2015-10-26 ((2457322j,0s,0n),+0s,2299161j)>
> now -7
> => #<Date: 2015-10-19 ((2457315j,0s,0n),+0s,2299161j)>

If `x` is a reference to a local variable, `x -y` is interpreted as `x - y`.
Otherwise, `x -y` is interpreted as `x(-y)`.

    def x(y)
      :x
    end
    
    p(x -2) #=> :x
    x = 1
    p(x -2) #=> -1

This behavior is irrelevant to `Date`, so if you don't like it, please file another ticket.


----------------------------------------
Bug #11621: Date.- and Date.+ methods inconsistencies
https://bugs.ruby-lang.org/issues/11621#change-54576

* Author: Andrei Balcanasu
* Status: Rejected
* Priority: Normal
* Assignee: 
* ruby -v: ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
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)


-- 
https://bugs.ruby-lang.org/