From: David Vallner Date: 2006-12-03T19:53:36+09:00 Subject: Re: Simple and stuppid bug (can anyone find it?) --------------enigC2A67252DAFBD93047DFE80A Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable paul wrote: > Hi all, > I thought it to be good programming style to make attribute assignment > chainable. When using the attr_writer an assignment returns te value > assigned which is not what I want, so I tried to do-it-myself. > The result is below. This doesn't work. When running Ruby gives: > undefined method 'b=3D' for "AA":String (NoMethodError) > Anybody, please tell me: *why*. What simple stuppid thing am I > overseeing? > Thanks in advance! > Cheers, > Paul >=20 > --the code-- > class A > def initialize > end > def a > return @a > end > def a=3D(someA) > @a =3D someA > return self > end > def b > return @b > end > def b=3D(someB) > @b =3D someB > return self > end > end >=20 > puts A.new.a=3D("AA").b=3D("BB").a > --the code-- >=20 >=20 There is no bug in your code, apparently Ruby always returns the rvalue from what parses as an assignment. With your class loaded into irb, I got: irb(main):039:0> A.new.a=3D("AA") =3D> "AA" irb(main):043:0> A.new.send('a=3D', 'AA') # Does what you expected. =3D> # So, can anyone recall a rationale for the behaviour (consistency with mainstream programming languages with regards to assignment?) or is this a Ruby bug? Either way, disclaimer: I don't like what you're trying to do in the first place. Pretty much anyone reading your code will expect assignment to return the rvalue because that's what #attr_writer does, and the chained assignment in your example just reads plain ugly to me, especially since I subconsciously parse it as: A.new.a =3D "AA".b =3D "BB".a i.e. ``assign "BB".a to -both- "AA".b and A.new.a'' and therefore am in favour of consistency with assignment in other languages instead of consistency with Javaesque property setters (where the ambiguity doesn't manifest.) David Vallner --------------enigC2A67252DAFBD93047DFE80A Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (MingW32) iD8DBQFFcqysy6MhrS8astoRAgD1AJ9oERqfAqkIwBT9VJYSnnSfluQFXwCfUBKb 24fEAPvL1iiULSF9paVZyys= =i/Ea -----END PGP SIGNATURE----- --------------enigC2A67252DAFBD93047DFE80A--