unexpected error in time difference

H

Heesob Park

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 Heesob
 
J

Jorrel

If you place parentheses it's much clearer:

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

Robert Klemme

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top