Paul
Paul

Reputation: 31

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

Upvotes: 3

Views: 2618

Answers (2)

Pritesh Jain
Pritesh Jain

Reputation: 9146

use directly

j.test = 1,2,3

or

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

or `

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

Upvotes: 2

Boris Stitnicky
Boris Stitnicky

Reputation: 12578

Parsing of the Ruby interpreter. Try

j.send :test=, 1, 2, 3

Upvotes: 2

Related Questions