# Why does attr\_accessor in module require 'self.'?

**URL:** <https://rubytalk.org/t/why-does-attr-accessor-in-module-require-self/53088>\
**Category:** ruby-talk\
**Created:** [27 April 2009 22:30 UTC](https://rubytalk.org/t/why-does-attr-accessor-in-module-require-self/53088 "2009-04-27T22:30:30Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Hartin](https://avatars.discourse-cdn.com/v4/letter/b/5f8ce5/32.png) [@Brian\_Hartin](https://rubytalk.org/u/Brian_Hartin)\
**Post date:** [27 April 2009 22:30 UTC](https://rubytalk.org/t/why-does-attr-accessor-in-module-require-self/53088/1 "2009-04-27T22:30:30Z")

</div>

Hi there,

When a class mixes in a module containing 'attr\_accessor :foo', it must  
then use 'self.foo = ...' instead of simply 'foo = ...'. While I prefer  
'self.foo' for clarity's sake, I am stumped as to \_why\_ this is  
required. For example:

module A  
&nbsp;&nbsp;attr\_accessor :a  
end

class B  
&nbsp;&nbsp;include A

&nbsp;&nbsp;def bar  
&nbsp;&nbsp;&nbsp;&nbsp;a = "bar"  
&nbsp;&nbsp;end

&nbsp;&nbsp;def baz  
&nbsp;&nbsp;&nbsp;&nbsp;self.a = "baz"  
&nbsp;&nbsp;end  
end

module A  
&nbsp;&nbsp;attr\_accessor :a  
end

class B  
&nbsp;&nbsp;include A

&nbsp;&nbsp;def bar  
&nbsp;&nbsp;&nbsp;&nbsp;a = "bar"  
&nbsp;&nbsp;end

&nbsp;&nbsp;def baz  
&nbsp;&nbsp;&nbsp;&nbsp;self.a = "baz"  
&nbsp;&nbsp;end  
end

irb(main):050:0\> b = B.new  
=\> #\<B:0x2db69cc\>  
irb(main):051:0\> b.bar  
=\> "bar"  
irb(main):052:0\> b.a  
=\> nil  
irb(main):053:0\> b.baz  
=\> "baz"  
irb(main):054:0\> b.a  
=\> "baz"

Does anyone know why this is?

Thanks!

Brian Hartin

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![John\_Barnette1](https://avatars.discourse-cdn.com/v4/letter/j/e99b99/32.png) [@John\_Barnette1](https://rubytalk.org/u/John_Barnette1)\
**Post date:** [27 April 2009 22:39 UTC](https://rubytalk.org/t/why-does-attr-accessor-in-module-require-self/53088/2 "2009-04-27T22:39:54Z")

</div>

Hi,

> When a class mixes in a module containing 'attr\_accessor :foo', it must  
> then use 'self.foo = ...' instead of simply 'foo = ...'. While I prefer  
> 'self.foo' for clarity's sake, I am stumped as to \_why\_ this is  
> required. [snip]

It's because Ruby automatically creates local variables upon assignment. Given a class with a method "foo":

class C  
&nbsp;&nbsp;&nbsp;attr\_accessor :a

&nbsp;&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = 42 # this is a local variable assignment  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.a = 42 # this is a call to the "a=" method  
&nbsp;&nbsp;&nbsp;end  
end

~ j.

> **···**
>
> On Apr 27, 2009, at 3:30 PM, Brian Hartin wrote:

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [27 April 2009 22:48 UTC](https://rubytalk.org/t/why-does-attr-accessor-in-module-require-self/53088/3 "2009-04-27T22:48:17Z")

</div>

Brian Hartin wrote:

> Hi there,
> 
> When a class mixes in a module containing 'attr\_accessor :foo', it must  
> then use 'self.foo = ...' instead of simply 'foo = ...'. While I prefer  
> 'self.foo' for clarity's sake, I am stumped as to \_why\_ this is  
> required. For example:
> 
> module A  
> &nbsp;&nbsp;attr\_accessor :a  
> end
> 
> class B  
> &nbsp;&nbsp;include A
> 
> &nbsp;&nbsp;def bar  
> &nbsp;&nbsp;&nbsp;&nbsp;a = "bar"  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def baz  
> &nbsp;&nbsp;&nbsp;&nbsp;self.a = "baz"  
> &nbsp;&nbsp;end  
> end

In B#bar, ruby's parser sees "a = ..." and assumes that "a" is local variable, rather than attempting to call #a= (the writer method). This has the advantage that, since local variables always "shadow" methods of the same name, you can write code with the confidence that you are not invoking some method defined in the class (or somewhere up the ancestor chain) that you hadn't noticed. For one thing, this makes it easier to write code that knows as little as possible about its context, and hence can be shared around by Module#include or even copy-n-paste.

It is, however, a disadvantage for DSL design, but that's another story...

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [28 April 2009 12:26 UTC](https://rubytalk.org/t/why-does-attr-accessor-in-module-require-self/53088/4 "2009-04-28T12:26:26Z")

</div>

John Barnette wrote:

> It's because Ruby automatically creates local variables upon  
> assignment. Given a class with a method "foo":

Can attr\_accessor ever apply to variables that don't begin with @?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [28 April 2009 19:13 UTC](https://rubytalk.org/t/why-does-attr-accessor-in-module-require-self/53088/5 "2009-04-28T19:13:13Z")

</div>

no, otherwise it'd be named something else.

> **···**
>
> On Apr 28, 2009, at 05:26 , 7stud -- wrote:
> 
> > John Barnette wrote:
> > 
> > > It's because Ruby automatically creates local variables upon  
> > > assignment. Given a class with a method "foo":
> > 
> > Can attr\_accessor ever apply to variables that don't begin with @?

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [28 April 2009 21:35 UTC](https://rubytalk.org/t/why-does-attr-accessor-in-module-require-self/53088/6 "2009-04-28T21:35:05Z")

</div>

Ryan Davis wrote:

> **···**
>
> > On Apr 28, 2009, at 05:26 , 7stud -- wrote:
> > 
> > > John Barnette wrote:
> > > 
> > > > It's because Ruby automatically creates local variables upon  
> > > > assignment. Given a class with a method "foo":
> > > 
> > > Can attr\_accessor ever apply to variables that don't begin with @?
> > 
> > no, otherwise it'd be named something else.
> 
> Well, then what's all this talk about "the parser sees this, and the  
> parser does that". The op never tries to access an instance variable in  
> the classs, so the fact that a call to attr\_accessor is in the class is  
> irrelevant. End of story.  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [28 April 2009 22:14 UTC](https://rubytalk.org/t/why-does-attr-accessor-in-module-require-self/53088/7 "2009-04-28T22:14:29Z")

</div>

7stud -- wrote:

> Ryan Davis wrote:
> 
> > > John Barnette wrote:
> > > 
> > > > It's because Ruby automatically creates local variables upon  
> > > > assignment. Given a class with a method "foo":
> > > 
> > > Can attr\_accessor ever apply to variables that don't begin with @?
> > 
> > no, otherwise it'd be named something else.
> 
> Well, then what's all this talk about "the parser sees this, and the  
> parser does that". The op never tries to access an instance variable in  
> the classs, so the fact that a call to attr\_accessor is in the class is  
> irrelevant. End of story.

Hmm...not quite:

class B  
end

b = B.new  
puts b.a

--output:--  
undefined method `a' for #\<B:0x253c8\> (NoMethodError)

class B  
&nbsp;&nbsp;attr\_accessor :a  
end

b = B.new  
puts b.a

nil

> **···**
>
> > > On Apr 28, 2009, at 05:26 , 7stud -- wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [28 April 2009 23:16 UTC](https://rubytalk.org/t/why-does-attr-accessor-in-module-require-self/53088/8 "2009-04-28T23:16:42Z")

</div>

7stud -- wrote:

> > Well, then what's all this talk about "the parser sees this, and the  
> > parser does that".

If you do this:

class B  
&nbsp;&nbsp;def a  
&nbsp;&nbsp;&nbsp;&nbsp;@a  
&nbsp;&nbsp;end

&nbsp;&nbsp;def bar  
&nbsp;&nbsp;&nbsp;&nbsp;"bar"  
&nbsp;&nbsp;end

&nbsp;&nbsp;def baz  
&nbsp;&nbsp;&nbsp;&nbsp;self.a = "baz"  
&nbsp;&nbsp;end  
end

b = B.new  
puts b.bar  
puts b.a  
puts b.baz

--output:--  
bar  
nil  
'baz': undefined method `a=' for #\<B:0x24fe0\> (NoMethodError)

1) b.a returns nil because the 'a' method exists, but the 'a' method  
tries to return a non-existent instance variable @a.

2) The error message is due to the fact that self.a tries to access a  
method called a=, which in this class doesn't exist.

If you add the setter method, a=, then the error message goes away:

class B  
&nbsp;&nbsp;def a  
&nbsp;&nbsp;&nbsp;&nbsp;@a  
&nbsp;&nbsp;end

&nbsp;&nbsp;def a=(val)  
&nbsp;&nbsp;&nbsp;&nbsp;@a = val  
&nbsp;&nbsp;end

&nbsp;&nbsp;def bar  
&nbsp;&nbsp;&nbsp;&nbsp;"bar"  
&nbsp;&nbsp;end

&nbsp;&nbsp;def baz  
&nbsp;&nbsp;&nbsp;&nbsp;self.a = "baz"  
&nbsp;&nbsp;end  
end

b = B.new  
puts b.bar  
puts b.a  
puts b.baz

--output:--  
bar  
nil  
baz  
baz

Note that 'a =' doesn't appear anywhere in the code. Adding 'a =' in  
the body of a method doesn't change anything:

class B  
&nbsp;&nbsp;def a  
&nbsp;&nbsp;&nbsp;&nbsp;@a  
&nbsp;&nbsp;end

&nbsp;&nbsp;def a=(val)  
&nbsp;&nbsp;&nbsp;&nbsp;@a = val  
&nbsp;&nbsp;end

&nbsp;&nbsp;def bar  
&nbsp;&nbsp;&nbsp;&nbsp;a = "bar" #\<----\*\*\*\*  
&nbsp;&nbsp;end

&nbsp;&nbsp;def baz  
&nbsp;&nbsp;&nbsp;&nbsp;self.a = "baz"  
&nbsp;&nbsp;end

&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;a = 10  
&nbsp;&nbsp;end  
end

b = B.new  
puts b.bar  
puts b.a  
puts b.baz  
puts b.a

--output:--  
bar  
nil  
baz  
baz

All of which I guess has nothing to do with the op's question. The op  
correctly identified that self.a = "baz" calls the a= method, but that a  
= "bar" does not. I guess I would pose this question to the op: when  
have you ever been able to access the a= method in a class without using  
the format some\_obj.a = ?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [28 April 2009 23:34 UTC](https://rubytalk.org/t/why-does-attr-accessor-in-module-require-self/53088/9 "2009-04-28T23:34:42Z")

</div>

7stud -- wrote:

> class B  
> &nbsp;&nbsp;def a  
> &nbsp;&nbsp;&nbsp;&nbsp;@a  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def a=(val)  
> &nbsp;&nbsp;&nbsp;&nbsp;@a = val  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def bar  
> &nbsp;&nbsp;&nbsp;&nbsp;a = "bar" #\<----\*\*\*\*  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def baz  
> &nbsp;&nbsp;&nbsp;&nbsp;self.a = "baz"  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def foo  
> &nbsp;&nbsp;&nbsp;&nbsp;a = 10  
> &nbsp;&nbsp;end  
> end
> 
> b = B.new  
> puts b.bar  
> puts b.a  
> puts b.baz  
> puts b.a
> 
> --output:--  
> bar  
> nil  
> baz  
> baz
> 
> All of which I guess has nothing to do with the op's question. The op  
> correctly identified that self.a = "baz" calls the a= method, but that a  
> = "bar" does not. I guess I would pose this question to the op: when  
> have you ever been able to access the a= method in a class without using  
> the format some\_obj.a = ?

I guess the idea here:

> &nbsp;&nbsp;def bar  
> &nbsp;&nbsp;&nbsp;&nbsp;a = "bar" #\<----\*\*\*\*  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def baz  
> &nbsp;&nbsp;&nbsp;&nbsp;self.a = "baz"  
> &nbsp;&nbsp;end

is that when you call a method without a receiver, self is implied. So  
the bar method would be equivalent to:

> &nbsp;&nbsp;def bar  
> &nbsp;&nbsp;&nbsp;&nbsp;self. a = "bar" #\<----\*\*\*\*  
> &nbsp;&nbsp;end

Therefore bar and baz have the same format and should do the same thing.  
But...

John Barnette wrote:

> Ruby automatically creates local variables upon  
> assignment.

which might be explained further(though perhaps wrongly) by saying:  
before ruby can attach 'self.' to the front of 'a' in the bar method:

> &nbsp;&nbsp;def bar  
> &nbsp;&nbsp;&nbsp;&nbsp;a = "bar"  
> &nbsp;&nbsp;end

the parser tells ruby that it should create a local variable called 'a'  
instead and assign it the value on the right of the equals sign. As a  
result unadorned names, for example 'a' vs. 'obj.a', that appear to the  
left of an equals sign cause ruby to create a local variable and assign  
the local variable the value to the right of the equals sign.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Shaun\_Keller](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@Shaun\_Keller](https://rubytalk.org/u/Shaun_Keller)\
**Post date:** [29 April 2009 08:11 UTC](https://rubytalk.org/t/why-does-attr-accessor-in-module-require-self/53088/10 "2009-04-29T08:11:59Z")

</div>

The short answer would be that

a = 'bar'

is a local variable assignment, whereas

self.a = 'bar'

is an instance variable assignment, like @a

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [29 April 2009 08:35 UTC](https://rubytalk.org/t/why-does-attr-accessor-in-module-require-self/53088/11 "2009-04-29T08:35:36Z")

</div>

no. consider local variable assignment to be higher precedence than message send.

var = val

will ALWAYS be local variable assignment, even if you have #var=.

> **···**
>
> On Apr 28, 2009, at 16:34 , 7stud -- wrote:
> 
> > I guess the idea here:
> > 
> > > def bar  
> > > &nbsp;&nbsp;&nbsp;a = "bar" #\<----\*\*\*\*  
> > > end
> > > 
> > > def baz  
> > > &nbsp;&nbsp;&nbsp;self.a = "baz"  
> > > end
> > 
> > is that when you call a method without a receiver, self is implied. So  
> > the bar method would be equivalent to:
> > 
> > > def bar  
> > > &nbsp;&nbsp;&nbsp;self. a = "bar" #\<----\*\*\*\*  
> > > end
> > 
> > Therefore bar and baz have the same format and should do the same thing.  
> > But...

---

<div class="post-metadata">

**Author:** ![Florian\_Gilcher](https://avatars.discourse-cdn.com/v4/letter/f/57b2e6/32.png) [@Florian\_Gilcher](https://rubytalk.org/u/Florian_Gilcher)\
**Post date:** [29 April 2009 10:07 UTC](https://rubytalk.org/t/why-does-attr-accessor-in-module-require-self/53088/12 "2009-04-29T10:07:18Z")

</div>

No,

&nbsp;&nbsp;&nbsp;a = 'bar'

is local variable assignment, whereas

&nbsp;&nbsp;&nbsp;self.a = 'bar'

is a method call.

Where #a= was defined through

&nbsp;&nbsp;&nbsp;attr\_accessor :a

and has the side effect of assigning an @variable. It is totally possible to  
implement #a= without having it assign a variable - it is in no way special.

All you do is force that method call by explicitly mentioning a receiver.

> **···**
>
> On Apr 29, 2009, at 10:11 AM, Shaun Keller wrote:
> 
> > The short answer would be that
> > 
> > a = 'bar'
> > 
> > is a local variable assignment, whereas
> > 
> > self.a = 'bar'
> > 
> > is an instance variable assignment, like @a  
> > -- Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Brian\_Hartin](https://avatars.discourse-cdn.com/v4/letter/b/5f8ce5/32.png) [@Brian\_Hartin](https://rubytalk.org/u/Brian_Hartin)\
**Post date:** [29 April 2009 15:30 UTC](https://rubytalk.org/t/why-does-attr-accessor-in-module-require-self/53088/13 "2009-04-29T15:30:04Z")

</div>

The second and third posts have made it clear for me. This is necessary  
to allow local variables with the same name as instance variables. A  
contrasting example from a statically typed language makes this more  
clear (for me):

(Java)

public class Foo  
&nbsp;&nbsp;private int x;

&nbsp;&nbsp;public void bar() {  
&nbsp;&nbsp;&nbsp;&nbsp;int x = 2; // Very obviously a local variable  
&nbsp;&nbsp;}  
end

Since Ruby variables are allocated on first use, and Ruby has no  
explicit variable declaration syntax, there would be no way to have a  
local variable 'x' if an instance variable '@x' existed:

class Foo  
&nbsp;&nbsp;attr\_accessor :x

&nbsp;&nbsp;def bar  
&nbsp;&nbsp;&nbsp;&nbsp;# If this were the same as self.x = 2, or @x = 2,  
&nbsp;&nbsp;&nbsp;&nbsp;# it would not be possible to have a local variable 'x'?  
&nbsp;&nbsp;&nbsp;&nbsp;x = 2  
&nbsp;&nbsp;end  
end

Thanks much,

Brian Hartin

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [29 April 2009 15:43 UTC](https://rubytalk.org/t/why-does-attr-accessor-in-module-require-self/53088/14 "2009-04-29T15:43:33Z")

</div>

Hi --

Brian Hartin wrote:

> The second and third posts have made it clear for me. This is necessary  
> to allow local variables with the same name as instance variables. A  
> contrasting example from a statically typed language makes this more  
> clear (for me):
> 
> (Java)
> 
> public class Foo  
> &nbsp;&nbsp;&nbsp;private int x;
> 
> &nbsp;&nbsp;&nbsp;public void bar() {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int x = 2; // Very obviously a local variable  
> &nbsp;&nbsp;&nbsp;}  
> end
> 
> Since Ruby variables are allocated on first use, and Ruby has no  
> explicit variable declaration syntax, there would be no way to have a  
> local variable 'x' if an instance variable '@x' existed:
> 
> class Foo  
> &nbsp;&nbsp;&nbsp;attr\_accessor :x
> 
> &nbsp;&nbsp;&nbsp;def bar  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# If this were the same as self.x = 2, or @x = 2,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# it would not be possible to have a local variable 'x'?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = 2  
> &nbsp;&nbsp;&nbsp;end  
> end

Remember, though, that

&nbsp;&nbsp;&nbsp;@x = 2

and

&nbsp;&nbsp;&nbsp;obj.x = 2 # where obj may or may not be self

have nothing to do with each other. As Florian said, obj.x = 2 is a method call (the method being x=).

David

> **···**
>
> --  
> David A. Black / Ruby Power and Light, LLC  
> Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
> Now out in PDF: The Well-Grounded Rubyist ([http://manning.com/black2\](http://manning.com/black2%5C))  
> "Ruby 1.9: What You Need To Know" Envycasts with David A. Black  
> [http://www.envycasts.com](http://www.envycasts.com)
