From: 7stud -- Date: 2008-02-25T10:03:47+09:00 Subject: Re: Suprising behaviour with "def property=" method Farrel Lifson wrote: > I had a bit of a surprise with the following > > class Foo > def bar(value) > @bar = value.upcase > end > end > > f = Foo.new > my_bar = (f.bar = "bar") > > I initially thought that my_bar would == "BAR" but in fact it equals > "bar". It seems no matter what the final value of the bar= method is > the return value is always the parameters. First time I've run into > this so I thought I would share and ask if anyone can think of anyway > to get around this? > > Farrel I'm not sure why the return value of calling bar= is the method argument, but it makes sense to me that the return value is not the *private* instance variable's value. If the return value were the private instance variable's value, that would break the encapsulation that a class is supposed to provide: class Dog def secret_code=(seed) @secret_code = seed * 10 + 2 end end d = Dog.new return_val = (d.secret_code=(3) ) puts return_val #should this reveal the secret code? -- Posted via http://www.ruby-forum.com/.