From: Seebs Date: 2010-04-14T08:50:08+09:00 Subject: Re: I thought spaces didn't matter around operators On 2010-04-13, 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: Often, yes. > 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 Hmm! > Can someone explain what "undefined method `-@'" refers to? Probably "unary -". As in: > x = 3 > puts (-x) -3 But! > puts -x -3 > puts - x NoMethodError: undefined method `-' for nil:NilClass Basically, Ruby is interpreting "-x" as a unary minus on x, rather than as part of a larger expression. It turns out to be ambiguous in this case, because Ruby can't tell whether you mean (Time.now()) - (t) or (Time.now(-1 * t)) Only unary - isn't necessarily "-1 * t", it's just "whatever you get calling -@ on t". -s -- Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!