From: "David A. Black" Date: 2010-04-14T08:44:21+09:00 Subject: Re: I thought spaces didn't matter around operators Hi -- On Wed, 14 Apr 2010, Rob Biedenharn wrote: > > 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: >> > 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. Here's another interesting space/non-space case: >> def m(*args); "hi"; end => nil >> m *10 => "hi" >> m * 10 => "hihihihihihihihihihi" In m *10 the * is the unary *, while in m * 10 it's the infix operator/method *. David -- David A. Black Senior Developer, Cyrus Innovation Inc. THE COMPLEAT RUBYIST, Ruby training with Black/Brown/McAnally! http://www.compleatrubyist.com