4

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?

jrochkind
  • 22,799
  • 12
  • 59
  • 74
  • 2
    `"foo".to_s +'='` - in this case Ruby think that `+"="` is an argument for `to_s`. like: `"foo".to_s(+"=")` – fl00r Oct 27 '11 at 21:52
  • The second example has mismatching parentheses. Apart from that, I have the same behaviour in Ruby 1.8.7 p352 and 1.9.2 r32553 for all three code samples. – undur_gongor Oct 27 '11 at 21:56
  • If something can't be parsed by the compiler, it usually can't be parsed unambiguously by humans either. – Andrew Grimm Oct 27 '11 at 23:20

1 Answers1

3

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.

Leonid Shevtsov
  • 14,024
  • 9
  • 51
  • 82
  • As a sidenote: is there any 'formal' spec for the Ruby language? Backus-Naur or something? – Leonid Shevtsov Oct 27 '11 at 21:42
  • See [this question](http://stackoverflow.com/questions/663027/ruby-grammar). (Short answer: no) – Andrew Marshall Oct 27 '11 at 21:44
  • So such a parsing quirk can't be a bug by definition, since there is no 'correct' behavior of the parser. – Leonid Shevtsov Oct 27 '11 at 21:54
  • As far as I know, it's up to the core team to decide what's a bug and what's a "feature". Though *I suppose* they should be conforming to the official draft specification, but that's only officially for 1.8, not 1.9. – Andrew Marshall Oct 27 '11 at 22:09
  • Huh weird, I am getting the same behavior in 1.8.7 too. I swear I had an app I was upgrading from 1.8.7 to 1.9.2 that started raising that never raised before... apparently I didn't isolate the demonstration case properly though. I probably don't have the inclination to go look at my diffs/logs and figure out what was really going on at this point. Thanks! – jrochkind Oct 29 '11 at 03:30
  • Now I just wonder why it's coming up with "undefined method `+@'" as an error message, '@' ? – jrochkind Oct 29 '11 at 03:31
  • @jrochkind: Because that's the name of the message that corresponds to the unary prefix `+` operator. Obviously, the message *can't* be `+` since that is already the message for the *binary infix* `+` operator. So, the messages for the unary prefix `+` and `-` operators are `+@` and `-@`, respectively. Note that the message for the unary prefix `!` operator is simply `!` since there is no binary infix `!` operator, although it arguably should have been `!@` instead. – Jörg W Mittag Feb 09 '12 at 03:20