3

Passing one argument at a time into the parser method works. More than one and it generates "syntax error, unexpected ',', expecting ')' (SyntaxError)"

$array = []
array_1 = %w(tuna salmon herring)
array_2 = %w(crow owl eagle dove)

def parser (*argument)
  argument.each do |item|
    $array << item
  end
end

parser (array_1, array_2) # taking multiple arguments generates error
$array.flatten!
puts $array

1 Answer 1

5

It's the space before the parens. Remove it. Instead of treating array_1 and array_2 as args, it's treating it as a parenthesized expression (with the whole expression being one arg) and complaining about the comma. Your code should look like

parser(array_1, array_2)

Incidentally, Ruby 1.8.7 seems to simply emit a warning about this before behaving correctly.

Sign up to request clarification or add additional context in comments.

2 Comments

guess they changed behavior at 1.9 time?
@rogerdpack: Presumably so. I don't have Ruby 1.9 installed so I can't test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.