# Issue with: instance\_variable\_defined?

**URL:** <https://rubytalk.org/t/issue-with-instance-variable-defined/63037>\
**Category:** ruby-talk\
**Created:** [14 July 2011 01:03 UTC](https://rubytalk.org/t/issue-with-instance-variable-defined/63037 "2011-07-14T01:03:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HAL\_90001](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/hal_90001/32/2046_2.png) [@HAL\_90001](https://rubytalk.org/u/HAL_90001)\
**Post date:** [14 July 2011 01:03 UTC](https://rubytalk.org/t/issue-with-instance-variable-defined/63037/1 "2011-07-14T01:03:45Z")

</div>

I expect the third line to be true rather than false.

Comments?

Hal

class SomeClass  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :a1, :a2 # Creates @a1, a1, @a2, a2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_writer :b1, :b2 # Creates @b1, b1=, @b2, b2=  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :c1, :c2 # Creates @c1, c1, c1=, @c2, c2, c2=  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# ...  
&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;@c2 = 99  
&nbsp;&nbsp;end  
end

obj = SomeClass.new  
p obj.instance\_variable\_defined?(:@c2) # true  
p obj.instance\_variable\_defined?(:@d2) # false  
p obj.instance\_variable\_defined?(:@a2) # false :-/

---

<div class="post-metadata">

**Author:** ![Justin\_Collins](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@Justin\_Collins](https://rubytalk.org/u/Justin_Collins)\
**Post date:** [14 July 2011 01:34 UTC](https://rubytalk.org/t/issue-with-instance-variable-defined/63037/2 "2011-07-14T01:34:53Z")

</div>

I was going to say that the attr\_\* methods don't create instance variables, only the methods to read/write them, as that has been my understanding. But actually, the documentation (at least for attr\_reader and attr\_accessor) says they \_do\_ create instance variables.

[http://rdoc.info/stdlib/core/1.9.2/Module:attr\_accessor](http://rdoc.info/stdlib/core/1.9.2/Module:attr_accessor)

So, I don't know. Perhaps there is a difference in semantics between "create" and "define".

-Justin

> **···**
>
> On 07/13/2011 06:03 PM, Hal Fulton wrote:
> 
> > I expect the third line to be true rather than false.
> > 
> > Comments?
> > 
> > Hal
> > 
> > class SomeClass  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :a1, :a2 # Creates @a1, a1, @a2, a2  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_writer :b1, :b2 # Creates @b1, b1=, @b2, b2=  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :c1, :c2 # Creates @c1, c1, c1=, @c2, c2, c2=  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# ...  
> > &nbsp;&nbsp;&nbsp;def initialize  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@c2 = 99  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > obj = SomeClass.new  
> > p obj.instance\_variable\_defined?(:@c2) # true  
> > p obj.instance\_variable\_defined?(:@d2) # false  
> > p obj.instance\_variable\_defined?(:@a2) # false :-/

---

<div class="post-metadata">

**Author:** ![Tim\_Pease](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@Tim\_Pease](https://rubytalk.org/u/Tim_Pease)\
**Post date:** [14 July 2011 04:52 UTC](https://rubytalk.org/t/issue-with-instance-variable-defined/63037/3 "2011-07-14T04:52:37Z")

</div>

The behavior seems correct. The "rb\_attr" method in eval.c only creates a reader method and a writer method. It cannot initialize an instance variable since it operates at the class level, not the instance level.

In order to create the behavior you expect, the attr\_\* methods would have to inject "magic" into the object initialization sequence. That is the only way a class level method could initialize an instance variable for every object instantiated from the class. I do not see any evidence of such magic in the C-level "rb\_attr" method.

I believe you are getting a false value correctly.

&nbsp;&nbsp;p obj.instance\_variable\_defined?(:@b2) # false  
&nbsp;&nbsp;obj.b2 = 'foo'  
&nbsp;&nbsp;p obj.instance\_variable\_defined?(:@b2) # true

Since the :a2 accessor is only creating a reader, you will never be able to create an instance variable in the normal fashion ...

&nbsp;&nbsp;obj.instance\_variable\_set(:@a2, 'bar')  
&nbsp;&nbsp;p obj.instance\_variable\_defined?(:@a2) # true

But that is an obvious example to the point of absurdity.

Blessings,  
TwP

> **···**
>
> On Jul 13, 2011, at 7:03 PM, Hal Fulton wrote:
> 
> > I expect the third line to be true rather than false.
> > 
> > Comments?
> > 
> > Hal
> > 
> > class SomeClass  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :a1, :a2 # Creates @a1, a1, @a2, a2  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_writer :b1, :b2 # Creates @b1, b1=, @b2, b2=  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :c1, :c2 # Creates @c1, c1, c1=, @c2, c2, c2=  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# ...  
> > def initialize  
> > &nbsp;&nbsp;&nbsp;@c2 = 99  
> > end  
> > end
> > 
> > obj = SomeClass.new  
> > p obj.instance\_variable\_defined?(:@c2) # true  
> > p obj.instance\_variable\_defined?(:@d2) # false  
> > p obj.instance\_variable\_defined?(:@a2) # false :-/
