# Mixin Syntax for Newbies

**URL:** <https://rubytalk.org/t/mixin-syntax-for-newbies/27099>\
**Category:** ruby-talk\
**Created:** [2 May 2006 16:40 UTC](https://rubytalk.org/t/mixin-syntax-for-newbies/27099 "2006-05-02T16:40:22Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_O](https://avatars.discourse-cdn.com/v4/letter/n/e47774/32.png) [@Nathan\_O](https://rubytalk.org/u/Nathan_O)\
**Post date:** [2 May 2006 16:40 UTC](https://rubytalk.org/t/mixin-syntax-for-newbies/27099/1 "2006-05-02T16:40:22Z")

</div>

I'm trying to start using mixins and I'm having a little syntactual  
trouble, if that's a word.

Here's my test code:

> **···**
>
> ---------------  
> module One  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@one = "One!"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :one  
> end
> 
> class Two  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include One  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts @one  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> end
> 
> this = Two.new  
> ----------------
> 
> I have tried several variations on this, but Two.one always seems to  
> come out as "nil". Is it possible to set it to "One!" by default?
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [2 May 2006 16:51 UTC](https://rubytalk.org/t/mixin-syntax-for-newbies/27099/2 "2006-05-02T16:51:05Z")

</div>

First of all when you say  
module One  
&nbsp;&nbsp;&nbsp;&nbsp;@one = "One!"  
end

That sets the instance variable of the module One.

Second, this:

def initialize  
&nbsp;&nbsp;&nbsp;puts @one  
end

defeats the whole purpose of using attr\_reader

Finally, you may want to do something like:

module One  
&nbsp;&nbsp;&nbsp;def one  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@one ||= "One!" # sets @one to "One!" only if it's not already set  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# or is nil or false  
&nbsp;&nbsp;&nbsp;end  
end

class Two  
&nbsp;&nbsp;&nbsp;include One  
&nbsp;&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts one  
&nbsp;&nbsp;&nbsp;end  
end

> **···**
>
> On May 2, 2006, at 12:40 PM, Nathan Olberding wrote:
> 
> > I'm trying to start using mixins and I'm having a little syntactual  
> > trouble, if that's a word.
> > 
> > Here's my test code:
> > 
> > ---------------  
> > module One  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@one = "One!"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :one  
> > end
> > 
> > class Two  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include One  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts @one  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > this = Two.new  
> > ----------------
> > 
> > I have tried several variations on this, but Two.one always seems to  
> > come out as "nil". Is it possible to set it to "One!" by default?

---

<div class="post-metadata">

**Author:** ![Guest20](https://avatars.discourse-cdn.com/v4/letter/g/ee7513/32.png) [@Guest20](https://rubytalk.org/u/Guest20)\
**Post date:** [2 May 2006 16:53 UTC](https://rubytalk.org/t/mixin-syntax-for-newbies/27099/3 "2006-05-02T16:53:18Z")

</div>

Nathan Olberding wrote:

> I'm trying to start using mixins and I'm having a little syntactual  
> trouble, if that's a word.
> 
> Here's my test code:
> 
> ---------------  
> module One  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@one = "One!"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :one  
> end
> 
> class Two  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include One  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts @one  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> end
> 
> this = Two.new  
> ----------------
> 
> I have tried several variations on this, but Two.one always seems to  
> come out as "nil". Is it possible to set it to "One!" by default?

In the above code, @one is actually an instance variable of module One:

&nbsp;&nbsp;One.instance\_variable\_get "@one"  
&nbsp;&nbsp;# =\> "One!"

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

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [2 May 2006 16:54 UTC](https://rubytalk.org/t/mixin-syntax-for-newbies/27099/4 "2006-05-02T16:54:19Z")

</div>

> I'm trying to start using mixins and I'm having a little syntactual  
> trouble, if that's a word.
> 
> Here's my test code:
> 
> ---------------  
> module One  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@one = "One!"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :one  
> end
> 
> class Two  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include One  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts @one  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> end
> 
> this = Two.new  
> ----------------
> 
> I have tried several variations on this, but Two.one always seems to  
> come out as "nil". Is it possible to set it to "One!" by default?

\>\> module One  
\>\> attr\_writer :one  
\>\> def one  
\>\> @one || "One!"  
\>\> end  
\>\> end  
=\> nil  
\>\> class Two  
\>\> include One  
\>\> def initialize  
\>\> puts one  
\>\> end  
\>\> end  
=\> nil  
\>\> Two.new  
One!  
=\> #\<Two:0x328384\>

Hope that helps.

James Edward Gray II

> **···**
>
> On May 2, 2006, at 11:40 AM, Nathan Olberding wrote:

---

<div class="post-metadata">

**Author:** ![Dave\_Burt2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/dave_burt2/32/2036_2.png) [@Dave\_Burt2](https://rubytalk.org/u/Dave_Burt2)\
**Post date:** [2 May 2006 16:55 UTC](https://rubytalk.org/t/mixin-syntax-for-newbies/27099/5 "2006-05-02T16:55:19Z")

</div>

Nathan Olberding wrote:

> I'm trying to start using mixins and I'm having a little syntactual  
> trouble, if that's a word.
> 
> Here's my test code:
> 
> ---------------  
> module One  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@one = "One!"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :one  
> end
> 
> class Two  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include One  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts @one  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> end
> 
> this = Two.new  
> ----------------
> 
> I have tried several variations on this, but Two.one always seems to  
> come out as "nil". Is it possible to set it to "One!" by default?

Consider this simple alternative:

module One  
&nbsp;&nbsp;def one  
&nbsp;&nbsp;&nbsp;&nbsp;@one or "One!"  
&nbsp;&nbsp;end  
end

If that's not good enough, and you don't mind using a library rather  
than bending Ruby's modules to your will yourself, try Ara T. Howard's  
Traits library; it deals with this completely.

If you do want to delve into the how of it, check out Ruby Quiz 67:  
metakoans.rb and the submitted solutions.

[http://rubyquiz.com/quiz67.html](http://rubyquiz.com/quiz67.html)

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![Pat\_Maddox1](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@Pat\_Maddox1](https://rubytalk.org/u/Pat_Maddox1)\
**Post date:** [3 May 2006 23:51 UTC](https://rubytalk.org/t/mixin-syntax-for-newbies/27099/6 "2006-05-03T23:51:10Z")

</div>

Hey Nathan,

You should check out pp122-123 of Programming Ruby. [http://www.rubycentral.com/book/tut\_modules.html#UA](http://www.rubycentral.com/book/tut_modules.html#UA) will take you to  
the exact section that you should read.

Pat

> **···**
>
> On 5/2/06, Nathan Olberding \<nathan.olberding@gmail.com\> wrote:
> 
> > I'm trying to start using mixins and I'm having a little syntactual  
> > trouble, if that's a word.
> > 
> > Here's my test code:
> > 
> > ---------------  
> > module One  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@one = "One!"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :one  
> > end
> > 
> > class Two  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include One  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts @one  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > this = Two.new  
> > ----------------
> > 
> > I have tried several variations on this, but Two.one always seems to  
> > come out as "nil". Is it possible to set it to "One!" by default?
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Nathan\_O](https://avatars.discourse-cdn.com/v4/letter/n/e47774/32.png) [@Nathan\_O](https://rubytalk.org/u/Nathan_O)\
**Post date:** [2 May 2006 17:37 UTC](https://rubytalk.org/t/mixin-syntax-for-newbies/27099/7 "2006-05-02T17:37:39Z")

</div>

Simen wrote:

> In the above code, @one is actually an instance variable of module One:
> 
> &nbsp;&nbsp;One.instance\_variable\_get "@one"  
> &nbsp;&nbsp;# =\> "One!"

Okay, that makes sense. In that case, is it possible to have instance  
variables for classes defined in modules which they mix in?

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

---

<div class="post-metadata">

**Author:** ![Nathan\_O](https://avatars.discourse-cdn.com/v4/letter/n/e47774/32.png) [@Nathan\_O](https://rubytalk.org/u/Nathan_O)\
**Post date:** [3 May 2006 17:09 UTC](https://rubytalk.org/t/mixin-syntax-for-newbies/27099/8 "2006-05-03T17:09:00Z")

</div>

Dave Burt wrote:

> Consider this simple alternative:
> 
> module One  
> &nbsp;&nbsp;def one  
> &nbsp;&nbsp;&nbsp;&nbsp;@one or "One!"  
> &nbsp;&nbsp;end  
> end

That might work... I theoretically have to define each variable as a  
method? There's no way to initialize variables in modules and have those  
values appear in inheriting classes?

> If that's not good enough, and you don't mind using a library rather  
> than bending Ruby's modules to your will yourself, try Ara T. Howard's  
> Traits library; it deals with this completely.

I think I might have to.

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

---

<div class="post-metadata">

**Author:** ![Guest20](https://avatars.discourse-cdn.com/v4/letter/g/ee7513/32.png) [@Guest20](https://rubytalk.org/u/Guest20)\
**Post date:** [2 May 2006 18:31 UTC](https://rubytalk.org/t/mixin-syntax-for-newbies/27099/9 "2006-05-02T18:31:40Z")

</div>

Nathan Olberding wrote:

> Simen wrote:
> 
> > In the above code, @one is actually an instance variable of module One:
> > 
> > &nbsp;&nbsp;One.instance\_variable\_get "@one"  
> > &nbsp;&nbsp;# =\> "One!"
> 
> Okay, that makes sense. In that case, is it possible to have instance  
> variables for classes defined in modules which they mix in?

If you make it a method, you can call it from initialize:

&nbsp;&nbsp;module One  
&nbsp;&nbsp;&nbsp;&nbsp;def one; @one = "One!"; end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;class Two  
&nbsp;&nbsp;&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;one  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts @one  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;# Outputs "One!"  
&nbsp;&nbsp;two = Two.new

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

---

<div class="post-metadata">

**Author:** ![Nathan\_O](https://avatars.discourse-cdn.com/v4/letter/n/e47774/32.png) [@Nathan\_O](https://rubytalk.org/u/Nathan_O)\
**Post date:** [3 May 2006 17:10 UTC](https://rubytalk.org/t/mixin-syntax-for-newbies/27099/10 "2006-05-03T17:10:32Z")

</div>

Nathan Olberding wrote:

> Dave Burt wrote:
> 
> > Consider this simple alternative:
> > 
> > module One  
> > &nbsp;&nbsp;def one  
> > &nbsp;&nbsp;&nbsp;&nbsp;@one or "One!"  
> > &nbsp;&nbsp;end  
> > end
> 
> That might work... I theoretically have to define each variable as a  
> method? There's no way to initialize variables in modules and have those  
> values appear in inheriting classes?

> I think I might have to.

Better yet, should I be trying to use superclasses for this?

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

---

<div class="post-metadata">

**Author:** ![Guest20](https://avatars.discourse-cdn.com/v4/letter/g/ee7513/32.png) [@Guest20](https://rubytalk.org/u/Guest20)\
**Post date:** [2 May 2006 18:33 UTC](https://rubytalk.org/t/mixin-syntax-for-newbies/27099/11 "2006-05-02T18:33:18Z")

</div>

Simen wrote:

> Nathan Olberding wrote:
> 
> > Simen wrote:
> > 
> > > In the above code, @one is actually an instance variable of module One:
> > > 
> > > &nbsp;&nbsp;One.instance\_variable\_get "@one"  
> > > &nbsp;&nbsp;# =\> "One!"
> > 
> > Okay, that makes sense. In that case, is it possible to have instance  
> > variables for classes defined in modules which they mix in?
> 
> If you make it a method, you can call it from initialize:
> 
> &nbsp;&nbsp;module One  
> &nbsp;&nbsp;&nbsp;&nbsp;def one; @one = "One!"; end  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;class Two  
> &nbsp;&nbsp;&nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;one  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts @one  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;# Outputs "One!"  
> &nbsp;&nbsp;two = Two.new

Whoops, you need to include One first:

&nbsp;&nbsp;class Two  
&nbsp;&nbsp;&nbsp;&nbsp;include One  
&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;end

( I gotta stop using the ruby-forum gateway. It's so easy it makes me  
sloppy ).

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

---

<div class="post-metadata">

**Author:** ![Christophe\_Mckeon1](https://avatars.discourse-cdn.com/v4/letter/c/a88e57/32.png) [@Christophe\_Mckeon1](https://rubytalk.org/u/Christophe_Mckeon1)\
**Post date:** [3 May 2006 19:24 UTC](https://rubytalk.org/t/mixin-syntax-for-newbies/27099/12 "2006-05-03T19:24:21Z")

</div>

> Better yet, should I be trying to use superclasses for this?

maybe, i don't think there are any hard and fast rules for deciding when  
stuff should be in a module and when it should be in a class, but off  
the top of my head, and imho, a few rules of thumb might be:

1) avoid defining instance instance variables (as opposed to module  
instance variable) in mixin modules because they can potentialy clash  
with instance variables of instances of the class which included the  
module. of course it can be argued that method names will also clash,  
but instance variables are a little more intractable i think, and also  
why double the chances of bugs.

2) use module mixins when you want to be able to enhance classes with  
functionality regardless of their class structure.

3) note that in 2), i said functionality and not state. if you want  
state a better bet is to go with either inheritance or object  
composition.

4) break above rules freely if breaking them makes your life easier. for  
instance you might find yorself with two unrelated class hierarchies  
which have a common problem of state. if your module is not meant for  
widespread use like the Enumerable module, but only within your project  
and for your two class hierarchies then it would be convenient and also  
DRY to do it via a module mixin, but it should still be considered  
carefully as your requirements might change.

> > Is there a way to do this without having to re-initialize variables in  
> > each inheriting class?

ok i think this question and a few others reveal a fundamental confusion  
that you have, so i'll take it from the beginning. forget modules for a  
minute.

class X  
&nbsp;&nbsp;@foo = 'mama'  
&nbsp;&nbsp;def imethod  
&nbsp;&nbsp;&nbsp;&nbsp;puts @foo  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def self.cmethod  
&nbsp;&nbsp;&nbsp;&nbsp;puts @foo  
&nbsp;&nbsp;end  
end

in the above example 'class X' signals to the ruby runtime that a class  
is being built. from then on instance variables can be initialized (@foo  
= 'mama'), methods can be called and class and instance methods can be  
defined. note that the instance variable @foo is an instance variable of  
class X which is an object like any other.

X.cmethod

prints 'mama'

x = X.new  
x.imethod

prints nil or nothing because @foo has not yet been initialized in the  
instance of class X, x.

in other words class instance variables can be initialized and used  
either in the body of the class or in class methods, while instance  
variables are used and initialized in instance methods.

module M  
&nbsp;&nbsp;@foo = 'papa'  
end  
class X  
&nbsp;&nbsp;include M  
&nbsp;&nbsp;def self.cmethod  
&nbsp;&nbsp;&nbsp;&nbsp;puts @foo  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def imethod  
&nbsp;&nbsp;&nbsp;&nbsp;puts @foo  
&nbsp;&nbsp;end  
end

in the above example @foo is initialized in the module body and not by a  
method so it is an instance variable of the module. so when you write

X.cmethod

nothing is printed because in cmethod @foo references an instance  
variable of X which was never initialized. and when you write

x = X.new  
x.imethod

nothing is printed because @foo in this case references an instance  
variable of an instance of X, x in which again @foo was never  
initialized. note that if i had defined imethod in the module instead,  
it would have made absolutely no difference, it would still 'look for'  
an instance variable in x.  
i'd recommend getting your head firmly around the above before using a  
fancy metaprogramming library. hope my examples clear it up for you.

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

---

<div class="post-metadata">

**Author:** ![Nathan\_O](https://avatars.discourse-cdn.com/v4/letter/n/e47774/32.png) [@Nathan\_O](https://rubytalk.org/u/Nathan_O)\
**Post date:** [2 May 2006 21:04 UTC](https://rubytalk.org/t/mixin-syntax-for-newbies/27099/13 "2006-05-02T21:04:43Z")

</div>

Simen wrote:

> > If you make it a method, you can call it from initialize:
> > 
> > &nbsp;&nbsp;module One  
> > &nbsp;&nbsp;&nbsp;&nbsp;def one; @one = "One!"; end  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;class Two  
> > &nbsp;&nbsp;&nbsp;&nbsp;def initialize  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;one  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts @one  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;# Outputs "One!"  
> > &nbsp;&nbsp;two = Two.new
> 
> Whoops, you need to include One first:
> 
> &nbsp;&nbsp;class Two  
> &nbsp;&nbsp;&nbsp;&nbsp;include One  
> &nbsp;&nbsp;&nbsp;&nbsp;...  
> &nbsp;&nbsp;end

Is there a way to do this without having to re-initialize variables in  
each inheriting class?

> ( I gotta stop using the ruby-forum gateway. It's so easy it makes me  
> sloppy ).

Fast answers mean fast self-checking! It's a tradeoff I'm willing to  
live with.

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

---

<div class="post-metadata">

**Author:** ![Nathan\_O](https://avatars.discourse-cdn.com/v4/letter/n/e47774/32.png) [@Nathan\_O](https://rubytalk.org/u/Nathan_O)\
**Post date:** [3 May 2006 19:46 UTC](https://rubytalk.org/t/mixin-syntax-for-newbies/27099/14 "2006-05-03T19:46:07Z")

</div>

polypus wrote:

> ok i think this question and a few others reveal a fundamental confusion  
> that you have, so i'll take it from the beginning. forget modules for a  
> minute.

I'm not confused. Confusion would suggest I have some flawed knowledge,  
which would suggest that I have some knowledge at all, which isn't the  
case. This is a case where I'm downright \*ignorant\* 🙂

> class X  
> &nbsp;&nbsp;@foo = 'mama'  
> &nbsp;&nbsp;def imethod  
> &nbsp;&nbsp;&nbsp;&nbsp;puts @foo  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;def self.cmethod  
> &nbsp;&nbsp;&nbsp;&nbsp;puts @foo  
> &nbsp;&nbsp;end  
> end

... \<snip\>

I think I understand this much. I know that the specific implementations  
I've outlined above fail because the initialize method(s) aren't being  
called, and therefore nothing is being initialized. In a (tight!)  
nutshell, that seems to be the basic flaw.

The thing I'm trying to implement is pretty basic, so I'm very confident  
that variable names won't clash. In fact, method names \*would\* clash in  
a lot of cases as I find myself doing similar operations on different  
kinds of data, so I'm really trying to implement only instance variables  
in modules (or super classes, though I'm having a little trouble with my  
first googlings about how to use inheritance) and not methods.

So, is there a way to mix-in or inherit variables and have them  
initialize to a default state? I'm thinking this sounds like a job for  
super classes. I originally started doing this by googling for "multiple  
inheritance ruby", and that's when I found mixins, so that's why I  
started by asking about mixins.

Sorry, I just emanate noobiness 🙂

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

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [2 May 2006 21:34 UTC](https://rubytalk.org/t/mixin-syntax-for-newbies/27099/15 "2006-05-02T21:34:41Z")

</div>

This question is confusing me. Are you looking for class variables or instance variables? You initialize instance variables every time an instance is created. @one in this case is an instance variable.

> **···**
>
> On May 2, 2006, at 5:04 PM, Nathan Olberding wrote:
> 
> > Is there a way to do this without having to re-initialize variables in  
> > each inheriting class?

---

<div class="post-metadata">

**Author:** ![Christophe\_Mckeon1](https://avatars.discourse-cdn.com/v4/letter/c/a88e57/32.png) [@Christophe\_Mckeon1](https://rubytalk.org/u/Christophe_Mckeon1)\
**Post date:** [3 May 2006 20:54 UTC](https://rubytalk.org/t/mixin-syntax-for-newbies/27099/16 "2006-05-03T20:54:05Z")

</div>

> ...super classes, though I'm having a little trouble with my first googlings about how to use inheritance...

something like?

class X  
&nbsp;&nbsp;attr\_reader :foo, :bar  
&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;@foo, @bar = :mama, :papa  
&nbsp;&nbsp;end  
end

class Y \< X  
end

y = Y.new  
puts y.foo, y.bar

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

---

<div class="post-metadata">

**Author:** ![Nathan\_O](https://avatars.discourse-cdn.com/v4/letter/n/e47774/32.png) [@Nathan\_O](https://rubytalk.org/u/Nathan_O)\
**Post date:** [3 May 2006 20:58 UTC](https://rubytalk.org/t/mixin-syntax-for-newbies/27099/17 "2006-05-03T20:58:51Z")

</div>

polypus wrote:

> > ...super classes, though I'm having a little trouble with my first googlings about how to use inheritance...
> 
> something like?  
> ...

Exactly like that 🙂 I got way too sidetracked by the idea of modules.  
I'm off to start implementing something cool now. After that, I plan on  
studying up on the different uses of inheritance and mixins.

Thanks everyone for the boundless patience!

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