Wilson Bilkovich
12/12/2006 8:17:00 PM
On 12/12/06, Ryan Williams <mr.cruft@gmail.com> wrote:
> Assignment methods are weird, and I wish they acted more like regular
> methods. It seems like they could be, but the parser doesn't properly parse
> the usage. Let me show you what I mean.
>
> Assignment methods can't take more than one argument:
>
> class Test
> def d=(a,b)
> puts "A: #{a}, B: #{b}"
> end
> end
>
> test.d = 1, 2 #=>ArgumentError: wrong number of arguments (1 for 2)
> test.d = [1, 2] #=> ArgumentError: wrong number of arguments (1 for 2)
> test.d = *[1, 2] #=> ArgumentError: wrong number of arguments (1 for 2)
>
Here's what the parse tree looks like for:
test.d = 1
[:attrasgn, [:vcall, :test], :d=, [:array, [:lit, 1]]]
..and for:
test.d = 1,2
[:attrasgn, [:vcall, :test], :d=,
[:array, [:svalue, [:array, [:lit, 1], [:lit, 2]]]]]
As you can see in the first one.. even a single value passed to an
assignment is parsed as an array. If you need multiple arguments to an
assignment, you have to handle them inside the method body itself.