Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby *args syntax error

I found this weirdness that I would like to understand. If I define these two methods in pry...

def test(*args)
   puts args
end
def test=(*args)
    puts args
end

they both work.But if I put the above code in a module and include that module in another class (say, class Job), the following

j=Job.last
j.test=(1,2,3)

throws the following error...

SyntaxError: (irb):3: syntax error, unexpected ',', expecting ')'
j.test=(1,2,3)
          ^

The following work as expected...

j.test=[1,2,3]
j.test=(1)

So, it looks like inside the module, a method defined with an '=' always expects one arg. That doesn't make sense to me.

What am I missing

like image 567
Paul Avatar asked Dec 13 '25 12:12

Paul


2 Answers

Parsing of the Ruby interpreter. Try

j.send :test=, 1, 2, 3
like image 149
Boris Stitnicky Avatar answered Dec 15 '25 12:12

Boris Stitnicky


use directly

j.test = 1,2,3

or

j.test= ([1,2,3])

or `

j.send('test=',[1,2,3])  
like image 36
Pritesh Jain Avatar answered Dec 15 '25 13:12

Pritesh Jain