Why does this code work in ruby ​​1.8 and not ruby ​​1.9?

This piece of code:

def func *; end [func "hello"] 

parsed without errors in Ruby 1.8.7, but returns a syntax error:

 syntax error, unexpected ']', expecting '}' 

in Ruby> = 1.9. I looked at the difference between Ruby 1.8 and Ruby 1.9 , but could not find a link to this. Does anyone know what change causes this?

+6
source share
1 answer

To avoid ambiguity. Consider the following:

 def foo(a, b = 1) # foo takes an optional second argument end [foo 1, 2] 

This can be interpreted as [(foo 1), 2] or as [(foo 1, 2)] .

There are two references to calling methods (although they are not directly related to the array literal):

Note that parentheses are optional ... Except where there is a difference between use and the absence of parentheses

In many cases, parentheses are not needed when sending a message ... However, parentheses are needed to avoid ambiguity.

+5
source

Source: https://habr.com/ru/post/953563/


All Articles