# Attr\_\* position in a class and their results

**URL:** <https://www.ruby-forum.com/t/attr-position-in-a-class-and-their-results/112063>\
**Category:** Ruby\
**Created:** [August 14, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/attr-position-in-a-class-and-their-results/112063 "2007-08-14T21:05:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f9ae1b/32.png) [@jub](https://www.ruby-forum.com/u/jub)\
**Post date:** [August 14, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/attr-position-in-a-class-and-their-results/112063/1 "2007-08-14T21:05:53Z")

</div>

Hello,  
I was playing with instance variables when I made up those two classes :

class Vartest\_before  
def init  
@instance\_variable = “I belong to the instance”  
end

attr\_accessor :instance\_variable

def instance\_variable  
@instance\_variable = “Forced”  
end  
end

class Vartest\_after  
def init  
@instance\_variable = “I belong to the instance”  
end

def instance\_variable  
@instance\_variable = “Forced”  
end

attr\_accessor :instance\_variable  
end

Now when using them, here are the results (# =\>) :

a = Vartest\_after.new  
a.init  
b = Vartest\_before.new  
b.init

puts a.instance\_variable # =\> I belong to the instance  
a.instance\_variable = “Changed”  
puts a.instance\_variable # =\> Changed

puts b.instance\_variable # =\> Forced  
b.instance\_variable = “Changed”  
puts b.instance\_variable # =\> Forced

I don’t understand what’s happening 😕 Can you ?

---

<div class="post-metadata">

**Author:** ![jub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f9ae1b/32.png) [@jub](https://www.ruby-forum.com/u/jub)\
**Post date:** [August 14, 2007, 9:27pm UTC](https://www.ruby-forum.com/t/attr-position-in-a-class-and-their-results/112063/2 "2007-08-14T21:27:59Z")

</div>

Arno J. wrote:

> class Vartest\_before  
> def init  
> @instance\_variable = “I belong to the instance”  
> end
> 
> attr\_accessor :instance\_variable

This defines a method instance\_variable

> def instance\_variable  
> @instance\_variable = “Forced”  
> end  
> end

This defines the method instance\_variable again. The version created by  
attr\_accessor doesn’t exist anymore.

> end  
> This time it’s the other way around: First you define the method with  
> def, then you redefine it with attr\_accessor. It’s like doing  
> def bla()  
> 1  
> end  
> def bla()  
> 2  
> end  
> Everytime you call bla, it will return 2 because the second definition  
> overrides the first one.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![jub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f9ae1b/32.png) [@jub](https://www.ruby-forum.com/u/jub)\
**Post date:** [August 14, 2007, 9:33pm UTC](https://www.ruby-forum.com/t/attr-position-in-a-class-and-their-results/112063/3 "2007-08-14T21:33:22Z")

</div>

Ok, I’m must be tired because I was expecting just the opposite, which  
wasn’t logical.  
Sorry for this dumb question.

---

<div class="post-metadata">

**Author:** ![jub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f9ae1b/32.png) [@jub](https://www.ruby-forum.com/u/jub)\
**Post date:** [August 14, 2007, 9:50pm UTC](https://www.ruby-forum.com/t/attr-position-in-a-class-and-their-results/112063/4 "2007-08-14T21:50:01Z")

</div>

Alle martedÃ¬ 14 agosto 2007, Arno J. ha scritto:

> def instance\_variable  
> @instance\_variable = “Forced”  
> b.init  
> I don’t understand what’s happening 😕 Can you ?  
> In the case of Vartest\_before, when you call

puts b.instance\_variable

the value of @instance\_variable is changed to ‘Forced’ before being  
returned.  
If you substitute that line with

puts b.instance\_variable\_get(:@instance\_variable)

you’d get what (I think) you expect, that is ‘Changed’. This doesn’t  
happen  
for the other class because there your definition of instance\_variable  
is  
overwritten by the one provided by attr\_accessor.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![jub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f9ae1b/32.png) [@jub](https://www.ruby-forum.com/u/jub)\
**Post date:** [August 14, 2007, 11:01pm UTC](https://www.ruby-forum.com/t/attr-position-in-a-class-and-their-results/112063/5 "2007-08-14T23:01:08Z")

</div>

On 14.08.2007 21:34, Arno J. wrote:

> Ok, I’m must be tired because I was expecting just the opposite, which  
> wasn’t logical.

Maybe it was even caused by using a different order in class definition  
and usage. This got me confused initially as well. 🙂

> Sorry for this dumb question.

No prob.

Kind regards

robert
