From: Mark Hubbart Date: 2004-05-01T05:40:00+09:00 Subject: Re: def [](v) xx; return yy; end # returned value is ignored !? On Apr 30, 2004, at 11:58 AM, Jean-Hugues ROBERT wrote: > At 01:49 01/05/2004 +0900, you wrote: >> "Jean-Hugues ROBERT" 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). > > Humm, things are the way they are. When I get annoyed by my > kinds questions I end up answering "that's because it's been > that way for billion years". I am there quoting the E.T. answer > in movie Contact by Robert Zemeckis. It wasn't always that way. Calling "Foo.bar = value" used to respect #bar='s return value. Later, it was decided by matz that consistancy was best, and the behavior was changed to be consistant with the behavior of the normal assignment operator. One reason for this is that people would tend to expect this behavior, and perhaps not think about return values when writing the functions. So the following code might not do what is expected. class Foo def bar=(val) @bar = val.to_i end end f = Foo.new g = f.bar = h = "23" In this case, you would probably expect g, h and f.bar to have been assigned the same way. If the return value of #bar= is respected, however, h and g would have different values. cheers, --Mark