From: dblack@... Date: 2007-03-03T22:14:16+09:00 Subject: Re: Question about attr_accessor Hi -- On Sat, 3 Mar 2007, Roland Swingler wrote: > Hi, > > I came across something unexpected today, and wondered if anyone could > explain it to me. If I have this class: > > class Foo > def initialize > @bar = 0 > end > > attr_accessor :bar > > def add > bar += 1 > end > end > > then the "add" function doesn't work - it complains about a nil > object. You have to specifically put "self.bar += 1". Why is that - I > thought that the "self" was implicitly there? For methods that end in = (like bar=), you always need an explicit receiver, even if it's self, because otherwise Ruby will see it as an assignment to a local variable. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)