# Mixins

**URL:** <https://www.ruby-forum.com/t/mixins/136145>\
**Category:** Ruby\
**Created:** [April 29, 2008, 12:01pm UTC](https://www.ruby-forum.com/t/mixins/136145 "2008-04-29T12:01:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [April 29, 2008, 12:01pm UTC](https://www.ruby-forum.com/t/mixins/136145/1 "2008-04-29T12:01:34Z")

</div>

Hi guys!!!  
My question is:

Is any way to define variables in module which can be further used in  
mixin? The problem is that module I wanted to include into class has  
constant variables defined which are used within function it implements.  
I’m looking for simplest way to overwrite the variables from the class  
(may change the variables to non constant ones) the same meaning not to  
change implementation of functions from the module. See:

module Mod  
AnyVar = 10

def func  
#the method use AnyVar  
end  
end

class MyClass  
include Mod

def initialize  
#read configuration

#how to make the function use new AnyVar value without changing its  
implementation and parameters list  
func()  
end  
end

Thanks a lot for any ideas.

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [April 29, 2008, 12:20pm UTC](https://www.ruby-forum.com/t/mixins/136145/2 "2008-04-29T12:20:49Z")

</div>

I don’t know if I get the point but this seems work:

module Mod  
@anyvar = 10

def func  
puts @anyvar  
end  
end

class MyClass  
include Mod

def initialize  
#read configuration  
@anyvar = 2  
func()  
end

end

MyClass.new

# =\> 2

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [April 29, 2008, 12:35pm UTC](https://www.ruby-forum.com/t/mixins/136145/3 "2008-04-29T12:35:02Z")

</div>

Yep, you’re my hero 😉 Works well.

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [April 29, 2008, 1:04pm UTC](https://www.ruby-forum.com/t/mixins/136145/4 "2008-04-29T13:04:36Z")

</div>

Hi –

On Tue, 29 Apr 2008, Sandro P. wrote:

> I don’t know if I get the point but this seems work:
> 
> module Mod  
> @anyvar = 10
> 
> def func  
> puts @anyvar  
> end  
> end

You’ve got two variables called @anyvar in that code, and they have no  
relation to each other.

The first one is an instance variable belonging to the module Mod  
(that is, the actual object Mod). The second one represents an  
instance variable that will belong to each object that executes the  
func method.

So the @anyvar = 10 line is serving no purpose.

David

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [April 29, 2008, 2:26pm UTC](https://www.ruby-forum.com/t/mixins/136145/5 "2008-04-29T14:26:32Z")

</div>

Well, maybe you can try this way…

module Mod

def Mod.included(mod)  
mod.class\_eval \<\<-EOS  
alias\_method :old\_initialize, :initialize  
def initialize  
@anyvar = 10  
old\_initialize  
end  
EOS  
end

def func  
puts @anyvar  
end  
end

class MyClass

def initialize  
#read configuration  
puts @anyvar  
@anyvar = 2  
func()  
end

include Mod  
end

MyClass.new

# =\> 10

# =\> 2

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [April 29, 2008, 9:05pm UTC](https://www.ruby-forum.com/t/mixins/136145/6 "2008-04-29T21:05:14Z")

</div>

Marcin T. wrote:

> module Mod  
> def initialize

I am not sure I quite understand what you are trying to do, but you can  
do something like this:

module A  
def initialize  
super  
@var = 1  
end  
end

class B  
include A

```
    def initialize
            super
    end

    def check_var
            puts @var
    end

```

end

B.new.check\_var #=\> 1

The trouble is you need to make sure you call ‘super’ religiously if you  
are including a lot of modules.

-Justin
