From: Rob Biedenharn Date: 2010-04-14T08:31:33+09:00 Subject: Re: I thought spaces didn't matter around operators On Apr 13, 2010, at 7:24 PM, Sarah Allen wrote: > I had understood that operators, like minus (-), had special > "syntactic > sugar" that allowed me to include or omit spaces around them like > this: > >>> 1-1 > => 0 >>> 1 -1 > => 0 >>> 1 - 1 > => 0 >>> 1- 1 > => 0 > > However, in some cases, spaces seem to matter: > >>> t = Time.now > => Tue Apr 13 13:40:24 -0700 2010 >>> Time.now -t > NoMethodError: undefined method `-@' for Tue Apr 13 13:40:24 -0700 > 2010:Time > from (irb):6 >>> Time.now - t > => 6.895787 > > Can someone explain what "undefined method `-@'" refers to? > > Here's my version of Ruby if it matters: > $ ruby -v > ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0] > > Thanks, > Sarah > http://www.ultrasarus.com > -- > Posted via http://www.ruby-forum.com/. > irb> t=Time.now => Tue Apr 13 19:27:43 -0400 2010 irb> Time.now() -t => 5.824652 irb> Time.now -t NoMethodError: undefined method `-@' for Tue Apr 13 19:27:43 -0400 2010:Time from (irb):5 from :0 irb> -t NoMethodError: undefined method `-@' for Tue Apr 13 19:27:43 -0400 2010:Time from (irb):6 from :0 The flexible syntax also lets you omit parentheses and in this case, the "-t" is parsed as an argument to the Time.now method. Putting the explicit (and empty) argument list on the call removes this ambiguity. The '-@' method is the unary minus method. The error message is because there is no method that negates a Time instance. -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com http://gaslightsoftware.com Rob@GaslightSoftware.com