# Mixin objectvariables

**URL:** <https://rubytalk.org/t/mixin-objectvariables/66762>\
**Category:** ruby-talk\
**Created:** [26 November 2012 00:09 UTC](https://rubytalk.org/t/mixin-objectvariables/66762 "2012-11-26T00:09:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [26 November 2012 00:09 UTC](https://rubytalk.org/t/mixin-objectvariables/66762/1 "2012-11-26T00:09:19Z")

</div>

Hi,

i tried to replace methods and variables common to many classes by a  
mixin. I attached the file that contains the module as well as the file  
where the module is mixed in. Here is a short schematic version of what  
i want to do.

module m  
&nbsp;&nbsp;@array = []  
&nbsp;&nbsp;def add x  
&nbsp;&nbsp;&nbsp;&nbsp;array.push x  
&nbsp;&nbsp;end  
end

class inner  
&nbsp;&nbsp;include m  
end

class outer  
&nbsp;&nbsp;inner.new.add x  
end

Here is the original error output.  
./updatepaintmodule.rb:21:in `addElement': undefined method `push' for  
nil:NilClass (NoMethodError)  
&nbsp;&nbsp;from ./layeredbg.rb:7:in `initialize'  
&nbsp;&nbsp;from ./game.rb:35:in `new'  
&nbsp;&nbsp;from ./game.rb:35:in `initialize'  
&nbsp;&nbsp;from ./main.rb:5:in `new'  
&nbsp;&nbsp;from ./main.rb:5

I think i saw an example where instance variables were mixed in. So, why  
would they be nil?

Attachments:  
[http://www.ruby-forum.com/attachment/7897/updatepaintmodule.rb](http://www.ruby-forum.com/attachment/7897/updatepaintmodule.rb)  
[http://www.ruby-forum.com/attachment/7898/layeredbg.rb](http://www.ruby-forum.com/attachment/7898/layeredbg.rb)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [26 November 2012 12:12 UTC](https://rubytalk.org/t/mixin-objectvariables/66762/2 "2012-11-26T12:12:05Z")

</div>

That does not work. You define instance variables of the module and  
not of instances of classes which include the module.

You have basically two options

1. use lazy initialization via an accessor

module UpdatePaintModule  
&nbsp;&nbsp;def updatables  
&nbsp;&nbsp;&nbsp;&nbsp;@updatables ||=   
&nbsp;&nbsp;end

&nbsp;&nbsp;def addUpdatable updatable  
&nbsp;&nbsp;&nbsp;&nbsp;updatables.push updatable  
&nbsp;&nbsp;end

&nbsp;&nbsp;def update  
&nbsp;&nbsp;&nbsp;&nbsp;updatables.each { |updatable| updatable.update if  
updatable.respond\_to? :update}  
&nbsp;&nbsp;end

end

2. write a constructor

module UpdatePaintModule  
&nbsp;&nbsp;def initialize(\*a, &b)  
&nbsp;&nbsp;&nbsp;&nbsp;super  
&nbsp;&nbsp;&nbsp;&nbsp;@updatables =   
&nbsp;&nbsp;end  
end

Note, for option 2 to work your class needs to invoke super properly  
in it's #initialize:

class X  
&nbsp;&nbsp;def initialize(a, b)  
&nbsp;&nbsp;&nbsp;&nbsp;super()  
&nbsp;&nbsp;&nbsp;&nbsp;@a = a  
&nbsp;&nbsp;&nbsp;&nbsp;@b = b  
&nbsp;&nbsp;end  
end

Btw, Ruby convention is to write #add\_updatable instead of #addUpdatable.

Kind regards

robert

> **···**
>
> On Mon, Nov 26, 2012 at 1:09 AM, Michael Sas \<lists@ruby-forum.com\> wrote:
> 
> > Hi,
> > 
> > i tried to replace methods and variables common to many classes by a  
> > mixin. I attached the file that contains the module as well as the file  
> > where the module is mixed in. Here is a short schematic version of what  
> > i want to do.
> > 
> > module m  
> > &nbsp;&nbsp;@array =   
> > &nbsp;&nbsp;def add x  
> > &nbsp;&nbsp;&nbsp;&nbsp;array.push x  
> > &nbsp;&nbsp;end  
> > end
> > 
> > class inner  
> > &nbsp;&nbsp;include m  
> > end
> > 
> > class outer  
> > &nbsp;&nbsp;inner.new.add x  
> > end
> > 
> > Here is the original error output.  
> > ./updatepaintmodule.rb:21:in `addElement': undefined method `push' for  
> > nil:NilClass (NoMethodError)  
> > &nbsp;&nbsp;from ./layeredbg.rb:7:in `initialize'  
> > &nbsp;&nbsp;from ./game.rb:35:in `new'  
> > &nbsp;&nbsp;from ./game.rb:35:in `initialize'  
> > &nbsp;&nbsp;from ./main.rb:5:in `new'  
> > &nbsp;&nbsp;from ./main.rb:5
> > 
> > I think i saw an example where instance variables were mixed in. So, why  
> > would they be nil?
> > 
> > Attachments:  
> > [http://www.ruby-forum.com/attachment/7897/updatepaintmodule.rb](http://www.ruby-forum.com/attachment/7897/updatepaintmodule.rb)
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [26 November 2012 14:35 UTC](https://rubytalk.org/t/mixin-objectvariables/66762/3 "2012-11-26T14:35:13Z")

</div>

Thank you Robert for explaining and providing a solution. That was a  
very good answer.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
