Ruby array element evaluation

Viewed 81

I like to know why in arrays when an element is a method call the existence of parentheses matters. I understand [] is a method call and I assumed the arguments to that method call would be evaluated first, so the existence of the parentheses shouldn't matter. But clearly it matters.

def double x
  x * 2
end

a = [1, 2+2] # works fine the array is [1, 4]
b = [1, double(2)] # also works fine the array is [1, 4]
c = [1, (double 2)] # also works fine the array is [1, 4]
d = [1, double 2]  # throws error syntax error, unexpected tINTEGER, expecting keyword_do or '{' or '('
[1, double 2]
0 Answers
Related