# What happens to instance variables in modules when they are

**URL:** <https://www.ruby-forum.com/t/what-happens-to-instance-variables-in-modules-when-they-are/91574>\
**Category:** Ruby\
**Created:** [April 10, 2007, 4:27pm UTC](https://www.ruby-forum.com/t/what-happens-to-instance-variables-in-modules-when-they-are/91574 "2007-04-10T16:27:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 10, 2007, 4:27pm UTC](https://www.ruby-forum.com/t/what-happens-to-instance-variables-in-modules-when-they-are/91574/1 "2007-04-10T16:27:18Z")

</div>

Hi,

Please take a look at the code pasted at this URL (I will include the  
code in the body of this message (below) for future readers, but the  
[rafb.net](http://rafb.net) post will give readers for the next 24 hours something better  
to look at): [http://rafb.net/p/9MkY3H79.html](http://rafb.net/p/9MkY3H79.html)

Why is it that an instance method of the module gets mixed into the  
class, but instance variables of the module do not get mixed in?

Whats the best strategy for writing instance methods in a module that  
depend on some state in the mixee?

Thanks very much,  
Mike.

And now here is the code snippet:

module M  
@module\_instance\_variable = ‘miv’

def module\_instance\_method  
@module\_instance\_variable  
end  
end

class C  
extend M  
end

puts “Is module\_instance\_method in the methods of the class?  
#{C.methods.include?(‘module\_instance\_method’)}”  
puts “Is @module\_instance\_variable in the instance variables of the  
class? #{C.instance\_variables.include?(‘@module\_instance\_variable’)}”  
puts “Is @module\_instance\_variable in the class variables of the  
class? #{C.class\_variables.include?(‘@module\_instance\_variable’)}”

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 10, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/what-happens-to-instance-variables-in-modules-when-they-are/91574/2 "2007-04-10T22:34:17Z")

</div>

On Tue, Apr 10, 2007 at 11:25:06PM +0900, removed\_email\_address@domain.invalid wrote:

> extend M  
> end

Presumably you understand why the following code prints ‘nil’:

class C  
@foo = “hello”

def bar  
puts @foo  
end  
end  
C.new.bar

So I’m not sure what you’re trying to achieve with your code snippet. Do  
you  
want @module\_instance\_variable to be an instance variable of module M,  
or an  
instance variable of class C, or an instance variable of instances of  
class  
C?

What you have at the moment is an instance variable of M, as shown by:

puts M.module\_eval { @module\_instance\_variable }

If you want to set up instance variables of class C itself (remembering  
that  
many different C’s may each extend M), then one approach is

module M  
def self.extended(obj)  
puts “hello!”  
obj.instance\_eval { @miv = “miv” }  
end  
end

class C  
extend M  
end

puts C.instance\_variables

HTH,

Brian.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 12, 2007, 5:18am UTC](https://www.ruby-forum.com/t/what-happens-to-instance-variables-in-modules-when-they-are/91574/3 "2007-04-12T05:18:14Z")

</div>

You know, there’s a downside to your e-mail addy, removed\_email\_address@domain.invalid. In  
Gmail it says the thread was started by me and I go, oh, hmm, I don’t  
remember posting that. Was I in a hypnotic trance? Was I possessed by  
demons? Did I post it a long long time ago?

(Fortunately the inevitable identity crisis resolves itself peacefully  
and uneventfully.)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 11, 2007, 10:06am UTC](https://www.ruby-forum.com/t/what-happens-to-instance-variables-in-modules-when-they-are/91574/4 "2007-04-11T10:06:12Z")

</div>

On Apr 10, 9:33 pm, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > And now here is the code snippet:  
> > extend M  
> > end
> 
> class C  
> extend M  
> end
> 
> puts C.instance\_variables
> 
> HTH,
> 
> Brian.

Ok - yes thats twigged it for me. Thanks very much Brian.

Mike
