# Problems with mixins

**URL:** <https://www.ruby-forum.com/t/problems-with-mixins/97865>\
**Category:** Ruby\
**Created:** [July 11, 2006, 12:05am UTC](https://www.ruby-forum.com/t/problems-with-mixins/97865 "2006-07-11T00:05:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [July 11, 2006, 12:05am UTC](https://www.ruby-forum.com/t/problems-with-mixins/97865/1 "2006-07-11T00:05:30Z")

</div>

Hi Folks,  
I’m have some problems with mixin from a module that I have  
written.

Module Parser

def thing  
.  
.  
end

end

class Outer  
include Parser

def initialize  
print thing # works  
end

class Inner

```
def initialize
  print thing # fails with undefined local variable or method 

```

`thing’  
end  
end # of Inner  
end # of Outer

I’ve tried an include Parser in inner but that does not make any  
difference

How can I access the mixins from embedded classes?

Cheers, Russell

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [July 11, 2006, 12:29am UTC](https://www.ruby-forum.com/t/problems-with-mixins/97865/2 "2006-07-11T00:29:20Z")

</div>

Hi –

On Tue, 11 Jul 2006, Russell F. wrote:

> def initialize  
> print thing # fails with undefined local variable or method  
> `thing’  
> end  
> end # of Inner  
> end # of Outer
> 
> I’ve tried an include Parser in inner but that does not make any  
> difference

It should. Try again 🙂

module Parser

def thing  
“Thing”  
end

end

class Outer  
include Parser

def initialize  
puts thing  
end

class Inner  
include Parser  
def initialize  
puts thing  
end  
end  
end

Outer::Inner.new # =\> Thing

David

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [July 11, 2006, 2:58am UTC](https://www.ruby-forum.com/t/problems-with-mixins/97865/3 "2006-07-11T02:58:25Z")

</div>

Hi –

On Tue, 11 Jul 2006, Russell F. wrote:

> include Parser
> 
> def initialize ()

The empty parens are definitely _not_ part of the prgram’s essentials  
🙂 (You weren’t doing them before. Is it something I said? 🙂

> puts lineno # prints 10  
> x = Inner.new  
> end
> 
> class Inner
> 
> def initializ

The ‘e’ on the end of initialize _is_, however, essential 🙂

> puts lineno # gives error

You still haven’t included Parser in Inner. If you don’t, instance of  
Inner won’t have Parser’s methods. (See my example again.)

> end  
> end

You’re missing an end here.

> Outer.new()

What it comes down to is that module inclusion is per class. A nested  
class is still a completely different class from the class it’s nested  
in, so you need to include the module in the inner class separately.

David

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [July 11, 2006, 2:46am UTC](https://www.ruby-forum.com/t/problems-with-mixins/97865/4 "2006-07-11T02:46:52Z")

</div>

unknown wrote:

> Hi –
> 
> > 
> 
> It should. Try again 🙂

Thanks David, I thought it should…

It would appear that the problem is related to how the control is passed  
between the classes. I am instantiating Inner from Outer…

I have reduced my program to its essentials:

module Parser  
def lineno  
10  
end  
end

class Outer  
include Parser

def initialize ()

```
puts lineno # prints 10
x = Inner.new

```

end

class Inner

def initializ  
puts lineno # gives error  
end  
end

Outer.new()

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [July 11, 2006, 3:24am UTC](https://www.ruby-forum.com/t/problems-with-mixins/97865/5 "2006-07-11T03:24:46Z")

</div>

Thanks again David!

> What it comes down to is that module inclusion is per class. A nested  
> class is still a completely different class from the class it’s nested  
> in, so you need to include the module in the inner class separately.

I swear that I tried putting includes in the inner modules at one stage,  
sigh…

But I have been fiddling with various things and must have been holding  
my mouth the wrong way at the time 🙂

After going back and adding ‘include Parser’ to all the inner modules I  
am now back in business. Originally Parser was a class and I was  
passing an instance of it to new for each inner class. I decided that  
doing it with a mixin was a better approach since I will never have more  
than one Parser at a time.

Russell
