From: Colin Bartlett Date: 2010-11-02T20:19:33+09:00 Subject: Re: Strange whitespace parsing behavior on Ruby 1.8.7 (patchlevel 249/302) --0015175113e280c86804941013f3 Content-Type: text/plain; charset=ISO-8859-1 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! *** the OP's irb session irb(main):062:0* x = '123456'=> "123456" irb(main):063:0> x.slice(1, x.size -2) SyntaxError: compile error(irb):63: syntax error, unexpected ')', expecting kDO_BLOCK from (irb):63 from :0 irb(main):064:0> x.slice(1, x.size - 2)=> "2345" irb(main):065:0> x.slice(1, x.size-2)=> "2345" irb(main):066:0> x.slice(1, x.size -2) SyntaxError: compile error(irb):66: syntax error, unexpected ')', expecting kDO_BLOCK from (irb):66 from :0 irb(main):067:0> x[x.size -3,1] ArgumentError: wrong number of arguments (2 for 0) from (irb):67:in `size' from (irb):67 from :0 irb(main):068:0> x[x.size - 3,1]=> "4" irb(main):069:0> def ok(a,b) irb(main):070:1> end=> nil irb(main):071:0> ok(x.size +3,4) ArgumentError: wrong number of arguments (2 for 0) from (irb):71:in `size' from (irb):71 from :0 irb(main):072:0> ok(4,x.size +3) SyntaxError: compile error(irb):72: syntax error, unexpected ')', expecting kDO_BLOCK from (irb):72 from :0 irb(main):074:0> ok(4,x.size + 3)=> nil --0015175113e280c86804941013f3--