From: Brian Candler Date: 2010-04-15T05:33:25+09:00 Subject: Re: I thought spaces didn't matter around operators Sarah Allen wrote: >>> 1 + 1 > is the same as >>> 1.send(:+, 1) Yes. >>> 1 +1 > is the same as...? Same again, as you have shown. > So, in what way is 1 +2 not like 1 +t (where t is a Time object). Well, it must be either (1) + (t) or (1) (+t) The Ruby parser is picking the second, even though it doesn't make sense at a higher level (you can't have two adjacent expressions without an intervening operator) That doesn't explain why 1 +2 is treated differently though. Actually, there is an optimisation that numeric literals have any unary prefix interpreted by the parser. That is, +1 is the value +1, not 1 with a unary plus applied. You can demonstrate it thus: >> class Integer >> def +@ >> puts "Whee!" >> self >> end >> end => nil >> +1 => 1 >> +(1) Whee! => 1 >> (BTW, I don't recommend redefining core class operators like this. If you redefine Fixnum#+, and don't preserve its existing semantics, nasty things will happen) The first case doesn't invoke the unary plus method, it's just absorbed into the literal number. So you'd think even more that 1 +1 would be taken as two adjacent integers, like 1 1 or 1 +t. Clearly, it isn't. Welcome to Ruby. It's like your granny - it might have some warts, but it's an old friend. -- Posted via http://www.ruby-forum.com/.