7stud -- said... > 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? > I concur; the OP's expected behaviour would break encapsulation. class Foo attr_reader :bar def bar=(value) @bar = value.upcase end end f = Foo.new my_bar = (f.bar = "bar") puts my_bar # "bar" puts f.bar # "BAR" g = Foo.new g.bar = "bar" puts g.bar # "BAR" -- Cheers, Marc