jrochkind
jrochkind

Reputation: 23317

ruby 1.9.x, weird parsing, what's going on?

I don't even know what heading to give this one. Can someone explain to me what the heck is going on here? This is a simplification of what I'm really doing, of course if this was it there'd be an easier way to do it, but why is ruby 1.9.x parser having trouble with something that works in 1.8.x and seems straightforward?

 (rdb:2) struct = Struct.new(:foo, :bar).new
 (rdb:2) p struct.send( ( "foo".to_s +'=') , "VALUE")
 NoMethodError Exception: undefined method `+@' for "=":String

WHAT? But this works fine:

 (rdb:2) struct.send( ("foo".to_s) +'=') , "VALUE")

Ah wait, so does this, I guess it needs a space between the '+' operator and second value now?

 (rdb:2) p struct.send( ( "foo".to_s + '=') , "VALUE")

What the heck? Ruby 1.8.x was fine with it now. Wait, ruby 1.9.x supports unary prefix operators or something, and it's saying there's no unary prefix operator "+" for string if I don't leave the space in?

HUH? Can anyone clear this up?

Upvotes: 4

Views: 150

Answers (1)

Leonid Shevtsov
Leonid Shevtsov

Reputation: 14179

Ruby 1.8.7 also supports unary +, and gives the same error for +'='.

I would assume something changed in the parsing logic, and a +'=' is parsed as an unary plus in your expression. I wouldn't consider that a bug.

Upvotes: 3

Related Questions