"Jean-Hugues ROBERT" <jean_hugues_robert / yahoo.com> schrieb im Newsbeitrag
news:6.0.1.1.0.20040430083010.01ce4ab8 / pop.mail.yahoo.com...
> Hi,
>
> For some class I am redefining operator []=(x). Works great.
>
> However when I use it, p xx[] = zz prints zz, not the value
> returned by my method. It makes some sense yet I would have
> thought that the responsibility for that should belong to
> my method. Am I missing something ?

Yes, it's defined that way (see below).

> class Test
>   def []=(x)
>     nil
>   end
> end
> p Test.new[] = "hello" # => "hello", I was expecting nil

Normally you want

def []=(idx, val)
  nil
end

And that will always print val regardless of the return value to enforce
coherence with the simple "x = foo" which always evaluates to foo.

irb(main):007:0> class Foo
irb(main):008:1>   def []=(idx, val)
irb(main):009:2>     p [idx, val]
irb(main):010:2>     nil
irb(main):011:2>   end
irb(main):012:1> end
=> nil
irb(main):013:0> Foo.new["a"]="b"
["a", "b"]
=> "b"

Regards

    robert