Unexpected error in time difference


#1

Hi,

I found some unexpected errors in time difference test.
Is this a bug or feature?

irb(main):001:0> Time.now-1
=> Thu Nov 27 16:19:35 +0900 2008
irb(main):002:0> Time.now -1
ArgumentError: wrong number of arguments (1 for 0)
from (irb):2:in initialize' from (irb):2:innow’
from (irb):2
irb(main):003:0> Time.now - 1
=> Thu Nov 27 16:19:40 +0900 2008
irb(main):004:0> t = Time.now
=> Thu Nov 27 16:19:48 +0900 2008
irb(main):005:0> Time.now-t
=> 7.250976
irb(main):006:0> Time.now -t
NoMethodError: undefined method `-@’ for Thu Nov 27 16:19:48 +0900
2008:Time
from (irb):6
from :0
irb(main):007:0> Time.now - t
=> 11.539909

Regards,

Park H.


#2

On 27.11.2008 08:29, Heesob P. wrote:

irb(main):003:0> Time.now - 1
=> 11.539909
Works as designed. With Time.now -1 the parser parses this as
Time.now(-1). Rule of thumb: use spaces around operators - makes your
code also more readable.

Kind regards

robert


#3

If you place parentheses it’s much clearer:

Time.now().-(1)
vs
Time.now(-1) # wrong num of args


#4

From: Heesob P. [mailto:removed_email_address@domain.invalid]
#–

irb(main):002:0> Time.now -1

ArgumentError: wrong number of arguments (1 for 0)

from (irb):2:in `initialize’

from (irb):2:in `now’

from (irb):2

#…

irb(main):006:0> Time.now -t

NoMethodError: undefined method `-@’ for Thu Nov 27 16:19:48

+0900 2008:Time

from (irb):6

from :0

fwiw, i like the dot for its power, emphasis, and lesser surprise.

Time.now .- 1
=> Fri Nov 28 00:03:38 +0800 2008
Time.now .-1
=> Fri Nov 28 00:03:57 +0800 2008
Time.now.-1
=> Fri Nov 28 00:04:00 +0800 2008

t=Time.now
=> Fri Nov 28 00:09:15 +0800 2008
-t
NoMethodError: undefined method -@' for Fri Nov 28 00:09:15 +0800 2008:Time from (irb):20 from :0 Time.now -t NoMethodError: undefined method-@’ for Fri Nov 28 00:09:15 +0800
2008:Time
from (irb):21
from :0

Time.now. -t
=> 18.928238