From: nobu@... Date: 2015-10-27T02:45:30+00:00 Subject: [ruby-core:71200] [Ruby trunk - Bug #11621] Date.- and Date.+ methods inconsistencies Issue #11621 has been updated by Nobuyoshi Nakada. If you were use `-w` option, you were warned. ~~~ $ ruby -wc -e 'Date.today -7' -e 'now = Date.today' -e 'p now -7' -e:1: warning: ambiguous first argument; put parentheses or a space even after `-' operator -e:3: warning: `-' after local variable or literal is interpreted as binary operator -e:3: warning: even though it seems like unary operator Syntax OK ~~~ ---------------------------------------- Bug #11621: Date.- and Date.+ methods inconsistencies https://bugs.ruby-lang.org/issues/11621#change-54578 * 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 => # irb(main):003:0> Date.today -7 => # irb(main):004:0> Date.today-7 => # ~~~ 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 => # 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 => # 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 `
' irb(main):004:0> Time.new(2001,2,3).to_date - 7 => # ~~~ 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/