From: Robert Klemme Date: 2010-11-02T23:04:38+09:00 Subject: Re: Strange whitespace parsing behavior on Ruby 1.8.7 (patchlevel 249/302) On Tue, Nov 2, 2010 at 12:19 PM, Colin Bartlett wrote: > On Tue, Nov 2, 2010 at 7:17 AM, Robert Klemme wrote: > >> >> Can you repost with proper indentation and line wrapping? >> Guessing from your text I assume you might have fallen >> into a unary / binary operator ambiguity. >> > > That seems to be a correct diagnosis. See below for the OP's irb session > with formatting. > For the OP: > > 7.abs + 3 #=> 10 > 7.abs+3   #=> 10 > > 7.abs +3   #=> ArgumentError: wrong number of arguments(1 for 0) > ## Ruby parses the "+" in " +3" as a unary + on 3 giving integer 3, > ## which means that integer 3 is an argument to abs, hence the error > message. > > def okp( a, b ); a + b; end  #=> nil > > okp(4, 7.abs + 3) #=> 14 > okp(4, 7.abs+3)   #=> 14 > okp(4, 7.abs +3)  #=> SyntaxError: (irb):23: syntax error, >                          #   unexpected tInteger, expecting ")" > ## I leave that - and the next - one to Robert to explain! > > x = '123456'  #=> "123456" > x.slice(1, x.size -2)  #=> SyntaxError: compile error(irb):63: >                             # syntax error, unexpected ')', >                             # expecting kDO_BLOCK >                             # from (irb):63 from :0 > ## Again, the "-" in " -2" is parsed on a unary on 2 giving integer value > -2. > ## Why the ')' is unexpected I leave to Robert! Very generous. :-) Concluding from systematic experimentation I believe both errors you left for me are related to the fact how Ruby parses (or rather: tries to parse) in absence of brackets: 14:53:27 $ ruby19 -ce 'a(b.c +1)' Syntax OK 14:53:45 $ ruby19 -ce 'a(d,b.c +1)' -e:1: syntax error, unexpected tINTEGER, expecting ')' a(d,b.c +1) ^ 14:53:50 $ ruby19 -ce 'a(b.c +1,e)' Syntax OK 14:53:55 $ ruby19 -ce 'a(d,b.c(+1))' Syntax OK 14:54:08 $ ruby19 -ce 'a((d,b.c)+1)' -e:1: syntax error, unexpected ')', expecting '=' a((d,b.c)+1) ^ -e:1: syntax error, unexpected $end, expecting ')' 14:59:30 $ ruby19 -ce 'a(d,(b.c) +1)' Syntax OK It seems that 14:53:45 is parsed like 14:54:08 which is apparently is illegal because you also cannot do this: 14:54:16 $ ruby19 -ce '(a,b)' -e:1: syntax error, unexpected '\n', expecting '=' 14:58:18 $ ruby19 -ce '(d,b.c +1)' -e:1: syntax error, unexpected tINTEGER, expecting tCOLON2 or '[' or '.' (d,b.c +1) ^ Hope that sheds some light. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/