# Question about attr\_accessor

**URL:** <https://www.ruby-forum.com/t/question-about-attr-accessor/87527>\
**Category:** Ruby\
**Created:** [March 3, 2007, 2:00pm UTC](https://www.ruby-forum.com/t/question-about-attr-accessor/87527 "2007-03-03T14:00:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![schmitr3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea666f/32.png) [@schmitr3](https://www.ruby-forum.com/u/schmitr3)\
**Post date:** [March 3, 2007, 2:00pm UTC](https://www.ruby-forum.com/t/question-about-attr-accessor/87527/1 "2007-03-03T14:00:43Z")

</div>

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?

Doing:

f = Foo.new  
f.add

works as the class stands, so you only seem to need the self if you  
are calling the accessor from within your class.

Confused…

TIA  
Roland

---

<div class="post-metadata">

**Author:** ![schmitr3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea666f/32.png) [@schmitr3](https://www.ruby-forum.com/u/schmitr3)\
**Post date:** [March 3, 2007, 2:09pm UTC](https://www.ruby-forum.com/t/question-about-attr-accessor/87527/2 "2007-03-03T14:09:44Z")

</div>

Change your add method to:

> def add  
> @bar+=1  
> end

If you use just ‘bar’ it will think of a local variable which is not  
defined.

martin

---

<div class="post-metadata">

**Author:** ![schmitr3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea666f/32.png) [@schmitr3](https://www.ruby-forum.com/u/schmitr3)\
**Post date:** [March 3, 2007, 2:12pm UTC](https://www.ruby-forum.com/t/question-about-attr-accessor/87527/3 "2007-03-03T14:12:44Z")

</div>

On 3/3/07, Roland S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> attr\_accessor :bar  
> Doing:  
> Roland

attr\_accessor :bar refers to the instance variable @bar.  
bar and @bar are not the same.

Harry

---

<div class="post-metadata">

**Author:** ![schmitr3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea666f/32.png) [@schmitr3](https://www.ruby-forum.com/u/schmitr3)\
**Post date:** [March 3, 2007, 2:14pm UTC](https://www.ruby-forum.com/t/question-about-attr-accessor/87527/4 "2007-03-03T14:14:44Z")

</div>

Hi –

On Sat, 3 Mar 2007, Roland S. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![schmitr3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea666f/32.png) [@schmitr3](https://www.ruby-forum.com/u/schmitr3)\
**Post date:** [March 3, 2007, 3:05pm UTC](https://www.ruby-forum.com/t/question-about-attr-accessor/87527/5 "2007-03-03T15:05:50Z")

</div>

Great - thanks for clearing that up.

---

<div class="post-metadata">

**Author:** ![schmitr3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea666f/32.png) [@schmitr3](https://www.ruby-forum.com/u/schmitr3)\
**Post date:** [March 3, 2007, 2:15pm UTC](https://www.ruby-forum.com/t/question-about-attr-accessor/87527/6 "2007-03-03T14:15:45Z")

</div>

On 03.03.2007 13:55, Roland S. wrote:

> 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?

bar += 1 is translated to “bar = bar + 1”. When Ruby sees “bar =” it  
takes bar to be a local variable. Hence you have to prefix with  
“self.”. See:  
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UO](http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UO)

Kind regards

robert
