I'm surprised by a particular bit of syntax-sensitivity in Ruby. These all work:
var = method arg
var2 = (var1 = method arg)
method2(method1 arg)
But this does not:
method2(var = method1 arg)
Instead, I have to do either this, with extra parentheses:
method2(var = method1(arg))
..or this, which I find much more ambiguous than the version that fails:
method2 var = method1(arg)
I assume that this is either a specific design decision or the side effect of another one, and would appreciate any insight into those decisions.
Please note that I'm not looking for any opinions about style; I'm not asking what looks better, or what you think should or should not work. I will even stipulate that this particular construct would be clearer if split into two separate statements entirely. I'm just curious about the actual reasons why Ruby works this way, from anyone who might have that background information.
I assume that this is either a specific design decision or the side effect of another one, and would appreciate any insight into those decisions.
Ruby's syntax is ridiculously complex. And since most Ruby implementations use a parser generator like Bison, which however isn't actually powerful enough to parse such a ridiculously complex language, the parsers tend to be even more ridiculously complex. It's much more likely that it's two weird parsing corner cases interacting in an even weirder way than any sort of conscious design decision.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With