# Accessing instance variable

**URL:** <https://www.ruby-forum.com/t/accessing-instance-variable/147400>\
**Category:** Ruby\
**Created:** [September 1, 2008, 2:06pm UTC](https://www.ruby-forum.com/t/accessing-instance-variable/147400 "2008-09-01T14:06:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mayuresh\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@Mayuresh\_K](https://www.ruby-forum.com/u/Mayuresh_K)\
**Post date:** [September 1, 2008, 2:06pm UTC](https://www.ruby-forum.com/t/accessing-instance-variable/147400/1 "2008-09-01T14:06:40Z")

</div>

Hello,

I did the following;  
class O  
@b = 0  
def x=(a)  
@b = a  
end  
end

o = O.new  
o.x = 1

I then tried to access b by doing;  
c = o.b

but irb gave me the following error;  
NoMethodError: undefined method `b' for #<O:0xb7aac424 @b=1> from (irb):10 from /usr/lib/ruby/1.9.0/irb.rb:150:in`block (2 levels) in  
eval\_input’  
from /usr/lib/ruby/1.9.0/irb.rb:259:in `signal_status' from /usr/lib/ruby/1.9.0/irb.rb:147:in`block in eval\_input’  
from /usr/lib/ruby/1.9.0/irb.rb:146:in `eval_input' from /usr/lib/ruby/1.9.0/irb.rb:70:in`block in start’  
from /usr/lib/ruby/1.9.0/irb.rb:69:in `catch' from /usr/lib/ruby/1.9.0/irb.rb:69:in`start’  
from /usr/bin/irb1.9:13:in `’

how do I access an instance variable if not via  
object.instance\_variable?

Best,

---

<div class="post-metadata">

**Author:** ![Mayuresh\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@Mayuresh\_K](https://www.ruby-forum.com/u/Mayuresh_K)\
**Post date:** [September 1, 2008, 2:09pm UTC](https://www.ruby-forum.com/t/accessing-instance-variable/147400/2 "2008-09-01T14:09:37Z")

</div>

2008/9/1 Mayuresh K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hello,
> 
> I did the following;  
> class O  
> @b = 0

You are not defining an instance variable of instances of O here but  
of O itself. In other words this @b and the @b below have nothing to  
do with each other.

> ```
> from /usr/bin/irb1.9:13:in `<main>'
> 
> ```
> 
> how do I access an instance variable if not via object.instance\_variable?

Please look up the docs for attr\_accessor, attr, attr\_reader and  
attr\_writer.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Mayuresh\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@Mayuresh\_K](https://www.ruby-forum.com/u/Mayuresh_K)\
**Post date:** [September 1, 2008, 3:15pm UTC](https://www.ruby-forum.com/t/accessing-instance-variable/147400/3 "2008-09-01T15:15:19Z")

</div>

Robert K. wrote:

> You are not defining an instance variable of instances of O here but  
> of O itself. In other words this @b and the @b below have nothing to  
> do with each other.

Exactly. And if you want to initialize @b for each new instance of O to  
the value of 0, you should write

class O  
def initialize  
@b = 0  
end  
def x=(a)  
@b = a  
end  
attr\_reader :b  
end

Now all new instances will have @b equal to 0. And the attr\_reader is  
for reading the instance variables. In fact attr\_reader :b is the same  
as writing explicitly

def b  
@b  
end

TPR.

---

<div class="post-metadata">

**Author:** ![Mayuresh\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@Mayuresh\_K](https://www.ruby-forum.com/u/Mayuresh_K)\
**Post date:** [September 1, 2008, 3:36pm UTC](https://www.ruby-forum.com/t/accessing-instance-variable/147400/4 "2008-09-01T15:36:01Z")

</div>

Additionally:

c = o.b

This always means: “run the _method_ ‘b’ on object ‘o’” - not “give me  
the instance variable called ‘b’ on object ‘o’”: instance variables are  
never in scope outside of the class definition. (So they would be  
‘private’ instance variables in Java for instance).

I just thought it was worth making that point - the stack trace you  
provided shows that this is going on - its complaining about ‘no such  
method’, rather than ‘no such instance variable’ or something like that.

This tallies up by the way with the fact that Ruby allows optional  
brackets (“parentheses” depending on where you are from) for method  
calls.

“c=o.b” is equivalent to “c=o.b()”.

So you get code that _looks_ like its reading a instance variable, but  
really its executing a method.

John

---

<div class="post-metadata">

**Author:** ![Mayuresh\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@Mayuresh\_K](https://www.ruby-forum.com/u/Mayuresh_K)\
**Post date:** [September 1, 2008, 4:49pm UTC](https://www.ruby-forum.com/t/accessing-instance-variable/147400/5 "2008-09-01T16:49:31Z")

</div>

> how do I access an instance variable if not via  
> object.instance\_variable?

To access instance variable, you need to define attribute  
reader/writer/accessor inside the class.

Attribute reader definition syntax

attr\_reader :a, :b, :c  
or  
attr\_reader ‘a’, ‘b’, ‘c’

Attribute writer definition syntax

attr\_writer :a, :b, :c  
or  
attr\_writer ‘a’, ‘b’, ‘c’

Attribute accessor (support read and write) definition syntax

attr\_accessor :a, :b, :c  
or  
attr\_accessor ‘a’, ‘b’, ‘c’

Example of Class Obj

class Obj  
attr\_reader :a  
attr\_writer :b  
attr\_accessor :c

def set\_a(param)  
@a = param  
end

def get\_b  
@b  
end  
end

a\_obj = Obj.new

a\_obj.set\_a = 1  
puts a\_obj.a

a\_obj.b = 2  
puts a\_obj.get\_b

a\_obj.c = 3  
puts a\_obj.c
