# Instance Variable in Mixins

**URL:** <https://www.ruby-forum.com/t/instance-variable-in-mixins/138076>\
**Category:** Ruby\
**Created:** [May 20, 2008, 1:00am UTC](https://www.ruby-forum.com/t/instance-variable-in-mixins/138076 "2008-05-20T01:00:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecd19e/32.png) [@dale](https://www.ruby-forum.com/u/dale)\
**Post date:** [May 20, 2008, 1:00am UTC](https://www.ruby-forum.com/t/instance-variable-in-mixins/138076/1 "2008-05-20T01:00:46Z")

</div>

I am trying to get access to an instance variable. I have simplified  
my usage down to the following example.

=== start-of-code

module A  
def A.a  
puts “A:#{@u}”  
end  
end

module B  
def b  
puts “B:#{@u}”  
end  
end

module C  
include A  
include B  
end

class Blah  
include C

def initialize(u)  
@u = u  
end

def go  
A.a  
b  
end  
end

blah = Blah.new(“bob”)

blah.go

=== end-of-code

My main question is that how can I get at the instance variable @u  
from within module A. I can use it fine from module B so I assume it  
is a namespace problem. It turns out that due to the way my mixins are  
used, I need to guard them against duplication. I guess I could use a  
prefix “def moduleA\_a … end” (El Yucko).

I am using ruby 1.8.6.

Thanks in advance,  
Dale

---

<div class="post-metadata">

**Author:** ![dale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecd19e/32.png) [@dale](https://www.ruby-forum.com/u/dale)\
**Post date:** [May 20, 2008, 1:37am UTC](https://www.ruby-forum.com/t/instance-variable-in-mixins/138076/2 "2008-05-20T01:37:04Z")

</div>

On Mon, May 19, 2008 at 7:00 PM, Dale M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am trying to get access to an instance variable. I have simplified  
> my usage down to the following example.

Hint. What do you think the difference is between:

> module A  
> def A.a

and

> module B  
> def b

Why do you need to write:

A.a  
but just  
b

in the go method in Blah?

Let’s instrument your code a little bit:

module A  
def A.a  
puts “self is #{self} A:#{@u}”  
end  
end

module B  
def b  
puts “self is #{self} B:#{@u}”  
end  
end

module C  
include A  
include B  
end

class Blah  
include C

def initialize(u)  
puts “Initializing #{self} u:#{u}”  
@u = u  
end

def go  
A.a  
b  
end  
end

blah = Blah.new(“bob”)

blah.go

RubyMate r8136 running Ruby r1.8.6  
(/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby)

> > > untitled

Initializing #Blah:0x8e2b0 u:bob  
self is A A:  
self is #Blah:0x8e2b0 B:bob  
Program exited.

The first method is defined ON the module A, not in the module, so  
it’s not a method of the instance of Blah, and within the A.a method  
@u refers to an instance variable of the object named A, i.e. the  
module itself, where as the b method, being in an instance method  
provided by the B module, is referring to the instance variable in the  
instance of Blah.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
