Ruby nested assignment and contactless method calls

I am surprised at the particular sensitivity to syntax in Ruby. All these works:

 var = method arg
 var2 = (var1 = method arg)
 method2(method1 arg)

      

But this is not the case:

method2(var = method1 arg)

      

Instead, I have to do 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 am assuming this is either a specific design decision or a side effect of another, and I would like to appreciate any understanding of these decisions.

Note that I am not looking for any style opinion; I'm not asking what looks best, or what you think should or shouldn't work. I will even point out that this particular construct would be clearer if it was completely split into two separate statements. I'm just curious about the real reasons why Ruby works this way from anyone who might have this reference.

+3


source to share


1 answer


I am assuming that this is either a specific design decision or a side effect of another, and would appreciate any understanding of these decisions.



Ruby's syntax is ridiculously complex. And since most Ruby implementations use a parser generator like Bison, which is, however, not really efficient enough to parse such a ridiculously complex language, parsers tend to be even more ridiculously complex . It is far more likely that the two weird corner case parsing will interact in an even weirder way than any conscious design decision.

+1


source







All Articles