Forum: Ruby unexpected error in time difference

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Heesob P. (Guest)
on 2008-11-27 09:34
(Received via mailing list)
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:in `now'
        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.
Robert K. (Guest)
on 2008-11-27 09:41
(Received via mailing list)
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
Jorrel (Guest)
on 2008-11-27 09:41
(Received via mailing list)
If you place parentheses it's much clearer:

Time.now().-(1)
vs
Time.now(-1)  # wrong num of args
Peña, Botp (Guest)
on 2008-11-27 10:01
(Received via mailing list)
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
This topic is locked and can not be replied to.