From: Josh Cheek Date: 2013-03-19T10:50:15+09:00 Subject: Re: Error in method call - need help to understand --047d7bb03c4ec7b76404d83d3a53 Content-Type: text/plain; charset=ISO-8859-1 On Mon, Mar 18, 2013 at 8:33 PM, Pritam Dey wrote: > I was trying to call a method as below: > > $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) > $array.flatten! > puts $array > > Error: > ===== > D:/Rubyscript/My ruby learning days/Scripts/test.rb:13: syntax error, u > nexpected ',', expecting ')' > parser (array_1,array_2) # taking multiple arguments generates error > ^ > > No I fixed the code by removing the space in the method call of parser > as below: > > $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 > > Output: > ======= > tuna > salmon > herring > crow > owl > eagle > dove > > But in the first method why such `space` causes the errors to be thrown > up? > > -- > Posted via http://www.ruby-forum.com/. > In Ruby, you do not have to use parens around a method. So `method(arg)` can be written as `method arg`. Lets say arg was some expression, you might want to put parens around it to make it clearer. `method (true && false)` which corresponds to `method((true && false))` So when you say `parser (array_1, array_2)`, that becomes `parser((array_1,array_2))` but `(array_1, array2)` is not a valid expression in Ruby. That is what is meant here ( http://stackoverflow.com/questions/15488899/how-to-pass-multiple-arguments-into-a-ruby-method) when he says "Instead of treating array_1 and array_2 as args, it's treating it as a parenthesized expression" -Josh --047d7bb03c4ec7b76404d83d3a53 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Mon, Mar 18, 2013 at 8:33 PM, Pritam Dey <lists@ruby-forum.com= > wrote:
I was trying to call a method as below:

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

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

parser (array_1,array_2)
$array.flatten!
puts $array

Error:
=3D=3D=3D=3D=3D
D:/Rubyscript/My ruby learning days/Scripts/test.rb:13: syntax error, u
nexpected ',', expecting ')'
parser (array_1,array_2) # taking multiple arguments generates error
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ^

No I fixed the code by removing the space in the method call of parser
as below:

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

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

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

Output:
=3D=3D=3D=3D=3D=3D=3D
tuna
salmon
herring
crow
owl
eagle
dove

But in the first method why such `space` causes the errors to be thrown
up?

--
Posted via http://= www.ruby-forum.com/.

In Ru= by, you do not have to use parens around a method. So `method(arg)` =A0can = be written as `method arg`. Lets say arg was some expression, you might wan= t to put parens around it to make it clearer. `method (true && fals= e)` which corresponds to `method((true && false))` So when you say = `parser (array_1, array_2)`, that becomes `parser((array_1,array_2))` but `= (array_1, array2)` is not a valid expression in Ruby.

That is what is meant here (http://stackoverflow.com/questions/15488899/how-to-pass-multiple-argume= nts-into-a-ruby-method) when he says "Instead of treating array_1 = and array_2 as args, it's treating it as a parenthesized expression&quo= t;

-Josh
--047d7bb03c4ec7b76404d83d3a53--