# Missing @ for instace variable in class, but no error message

**URL:** <https://www.ruby-forum.com/t/missing-for-instace-variable-in-class-but-no-error-message/196115>\
**Category:** Ruby\
**Created:** [October 7, 2010, 12:34am UTC](https://www.ruby-forum.com/t/missing-for-instace-variable-in-class-but-no-error-message/196115 "2010-10-07T00:34:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@stefans](https://www.ruby-forum.com/u/stefans)\
**Post date:** [October 7, 2010, 12:34am UTC](https://www.ruby-forum.com/t/missing-for-instace-variable-in-class-but-no-error-message/196115/1 "2010-10-07T00:34:04Z")

</div>

Some days ago I was defining a class, but forgot to use the leading @  
for an instance variable. But it still works – that variable was made  
accessible by attr\_accessor statement. So my question:

Why gives the statement “puts a” no error message? In my opinion it  
should give one, I forgot to use @. The “attr\_accessor :a” was only  
there to allow access from outside of that class, for instance  
variables.

#!/usr/bin/ruby -w  
class Test  
attr\_accessor :a  
def initialize  
@a = 0  
@b = 1  
end  
def show\_values  
puts a # should be @a, but seems to work  
puts b # gives an error, as expected  
end  
end  
t = Test.new  
t.show\_values

Output is:  
ruby --version  
ruby 1.8.7 (2010-08-16 patchlevel 302) [x86\_64-linux]

ruby test.rb  
0  
test.rb:11:in `show_values': undefined local variable or method`b’ for  
#\<Test:0x7f85f99c02f8 @b=1, @a=0\> (NameError)  
from test.rb:16

Best regards,

Stefan S.

---

<div class="post-metadata">

**Author:** ![stefans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@stefans](https://www.ruby-forum.com/u/stefans)\
**Post date:** [October 7, 2010, 12:38am UTC](https://www.ruby-forum.com/t/missing-for-instace-variable-in-class-but-no-error-message/196115/2 "2010-10-07T00:38:45Z")

</div>

attr\_accessor defines the Test#a method which is a getter for the @a  
instance variable

---

<div class="post-metadata">

**Author:** ![stefans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@stefans](https://www.ruby-forum.com/u/stefans)\
**Post date:** [October 7, 2010, 12:43am UTC](https://www.ruby-forum.com/t/missing-for-instace-variable-in-class-but-no-error-message/196115/3 "2010-10-07T00:43:26Z")

</div>

attr\_accessor actually defines a public instance method, named ‘a’. That  
method can be called from both inside and outside the class. So, it’s  
not an  
error, it’s perfectly valid.

---

<div class="post-metadata">

**Author:** ![stefans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@stefans](https://www.ruby-forum.com/u/stefans)\
**Post date:** [October 7, 2010, 12:53am UTC](https://www.ruby-forum.com/t/missing-for-instace-variable-in-class-but-no-error-message/196115/4 "2010-10-07T00:53:23Z")

</div>

On 10/6/2010 5:42 PM, Andrew W. wrote:

> attr\_accessor actually defines a public instance method, named ‘a’. That  
> method can be called from both inside and outside the class. So, it’s not an  
> error, it’s perfectly valid.

Beware of attempting to use the bare a= accessor within an instance  
method though. That will instead cause a variable named “a” to be  
created with the the method’s scope rather than use the a= accessor, so  
@a won’t be updated as expected. If you want to use the a= accessor,  
you need to use self.a = whatever. e.g.:

class MyClass  
attr\_accessor :a

def change\_a\_bad(value)  
a = value  
end

def change\_a\_good(value)  
self.a = value  
end  
end

my\_class = MyClass.new  
my\_class.a = 1

my\_class.change\_a\_bad(2)  
my\_class.a # =\> 1 !!!

my\_class.change\_a\_good(2)  
my\_class.a # =\> 2 🙂

-Jeremy

---

<div class="post-metadata">

**Author:** ![stefans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@stefans](https://www.ruby-forum.com/u/stefans)\
**Post date:** [October 7, 2010, 6:00am UTC](https://www.ruby-forum.com/t/missing-for-instace-variable-in-class-but-no-error-message/196115/5 "2010-10-07T06:00:38Z")

</div>

On 10/06/2010 10:17 PM, John S. wrote:

> people use ‘self.some\_method’ instead of just ‘some\_method’?".
> 
> I knew that I had a lot of redundant 'self’s and a few days ago I got  
> around to removing them in a script. As Jeremy points out, leaving off  
> ‘self’ in ‘some\_method=’ results in the creation of the variable  
> ‘some\_method’, as testing showed.

Thanks go to Dave T., Chad F., Andy H., and their book  
“Programming Ruby” – my introduction to Ruby. 🙂

-Jeremy

---

<div class="post-metadata">

**Author:** ![stefans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@stefans](https://www.ruby-forum.com/u/stefans)\
**Post date:** [October 7, 2010, 5:17am UTC](https://www.ruby-forum.com/t/missing-for-instace-variable-in-class-but-no-error-message/196115/6 "2010-10-07T05:17:58Z")

</div>

Jeremy B. wrote:

> Beware of attempting to use the bare a= accessor within an instance  
> method though. That will instead cause a variable named “a” to be  
> created with the the method’s scope rather than use the a= accessor, so  
> @a won’t be updated as expected. If you want to use the a= accessor,  
> you need to use self.a = whatever. e.g.:

Jeremy raises an excellent point. I wish I had read this post a few days  
ago. A few weeks ago, someone asked the question (paraphrased) “why do  
people use ‘self.some\_method’ instead of just ‘some\_method’?”.

I knew that I had a lot of redundant 'self’s and a few days ago I got  
around to removing them in a script. As Jeremy points out, leaving off  
‘self’ in ‘some\_method=’ results in the creation of the variable  
‘some\_method’, as testing showed.

js
