# Class Methods from Modules

**URL:** <https://rubytalk.org/t/class-methods-from-modules/42428>\
**Category:** ruby-talk\
**Created:** [22 November 2007 17:58 UTC](https://rubytalk.org/t/class-methods-from-modules/42428 "2007-11-22T17:58:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Greg\_Willits](https://avatars.discourse-cdn.com/v4/letter/g/cab0a1/32.png) [@Greg\_Willits](https://rubytalk.org/u/Greg_Willits)\
**Post date:** [22 November 2007 17:58 UTC](https://rubytalk.org/t/class-methods-from-modules/42428/1 "2007-11-22T17:58:19Z")

</div>

Can't see the tree for the forest.

Playing with how to integrate modules, classes, subclasses, and create  
instance methods and class methods from modules.

In this play code below, I want to use initialize\_colors in either the  
class Shape or Square as a class method.

At this point I'm sure I'm missing a very simple syntax rule explained  
very clearly in the books, but the code as is complains about  
initialize\_colors as being undefined, and I'm just not recognizing what  
I am missing.

module Color

&nbsp;&nbsp;def initialize\_colors  
&nbsp;&nbsp;&nbsp;&nbsp;@fill\_color = 'ffffff'  
&nbsp;&nbsp;&nbsp;&nbsp;@border\_color = '000000'  
&nbsp;&nbsp;end

&nbsp;&nbsp;def set\_fill\_rgb(color)  
&nbsp;&nbsp;&nbsp;&nbsp;if color == 'white'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@fill\_color = 'ffffff'  
&nbsp;&nbsp;&nbsp;&nbsp;elsif color == 'red'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@fill\_color = 'ff0000'  
&nbsp;&nbsp;&nbsp;&nbsp;elsif color == 'blue'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@fill\_color = '0000ff'  
&nbsp;&nbsp;&nbsp;&nbsp;elsif color == 'black'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@fill\_color = '000000'  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

class Shape

&nbsp;&nbsp;include Color

&nbsp;&nbsp;attr\_reader :fill\_color, :border\_color

&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;@fill\_color = String.new  
&nbsp;&nbsp;&nbsp;&nbsp;@border\_color = String.new  
&nbsp;&nbsp;end  
end

class Square \< Shape

&nbsp;&nbsp;initialize\_colors

end

sq = Square.new  
puts sq.fill\_color  
sq.set\_fill\_rgb('red')  
puts sq.fill\_color

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

---

<div class="post-metadata">

**Author:** ![Eric\_I2](https://avatars.discourse-cdn.com/v4/letter/e/22d042/32.png) [@Eric\_I2](https://rubytalk.org/u/Eric_I2)\
**Post date:** [22 November 2007 18:40 UTC](https://rubytalk.org/t/class-methods-from-modules/42428/2 "2007-11-22T18:40:03Z")

</div>

Hi Greg,

Well the methods you can call \*as\* a class is being defined are  
\*class\* methods. initialize\_colors is an instance method, as it  
should be since it's setting instance variables.

It seems to me that you want to call initialize\_colors not as the  
class is being defined but as instances are being created. And  
therefore the call to initialize\_colors should be inside Square's  
initialize method.

Eric

> **···**
>
> ====
> 
> Are you interested in on-site Ruby training that's been highly  
> reviewed by former students? [http://LearnRuby.com](http://LearnRuby.com)
> 
> On Nov 22, 12:58 pm, Greg Willits \<li...@gregwillits.ws\> wrote:
> 
> > Can't see the tree for the forest.
> > 
> > Playing with how to integrate modules, classes, subclasses, and create  
> > instance methods and class methods from modules.
> > 
> > In this play code below, I want to use initialize\_colors in either the  
> > class Shape or Square as a class method.
> > 
> > At this point I'm sure I'm missing a very simple syntax rule explained  
> > very clearly in the books, but the code as is complains about  
> > initialize\_colors as being undefined, and I'm just not recognizing what  
> > I am missing.
> > 
> > module Color
> > 
> > &nbsp;&nbsp;def initialize\_colors  
> > &nbsp;&nbsp;&nbsp;&nbsp;@fill\_color = 'ffffff'  
> > &nbsp;&nbsp;&nbsp;&nbsp;@border\_color = '000000'  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def set\_fill\_rgb(color)  
> > &nbsp;&nbsp;&nbsp;&nbsp;if color == 'white'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@fill\_color = 'ffffff'  
> > &nbsp;&nbsp;&nbsp;&nbsp;elsif color == 'red'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@fill\_color = 'ff0000'  
> > &nbsp;&nbsp;&nbsp;&nbsp;elsif color == 'blue'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@fill\_color = '0000ff'  
> > &nbsp;&nbsp;&nbsp;&nbsp;elsif color == 'black'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@fill\_color = '000000'  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > end
> > 
> > class Shape
> > 
> > &nbsp;&nbsp;include Color
> > 
> > &nbsp;&nbsp;attr\_reader :fill\_color, :border\_color
> > 
> > &nbsp;&nbsp;def initialize  
> > &nbsp;&nbsp;&nbsp;&nbsp;@fill\_color = String.new  
> > &nbsp;&nbsp;&nbsp;&nbsp;@border\_color = String.new  
> > &nbsp;&nbsp;end  
> > end
> > 
> > class Square \< Shape
> > 
> > &nbsp;&nbsp;initialize\_colors
> > 
> > end
> > 
> > sq = Square.new  
> > puts sq.fill\_color  
> > sq.set\_fill\_rgb('red')  
> > puts sq.fill\_color  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [23 November 2007 03:20 UTC](https://rubytalk.org/t/class-methods-from-modules/42428/3 "2007-11-23T03:20:00Z")

</div>

Greg Willits wrote:

> In this play code below, I want to use initialize\_colors in either the  
> class Shape or Square as a class method.

Including a module in a class causes instance methods of the module to  
be in the lookup path for instances of the class. Extending a class  
using a module causes instance methods of the module to be in the  
lookup path for the class itself.

(See [http://phrogz.net/RubyLibs/RubyMethodLookupFlow.png\](http://phrogz.net/RubyLibs/RubyMethodLookupFlow.png%5C))

A common idiom to be able to cause a class to get both class and  
instance methods when including a single module follows:

module Color  
&nbsp;&nbsp;module ClassMethods  
&nbsp;&nbsp;&nbsp;&nbsp;def initialize\_colors  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p "woot!"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;def giggle  
&nbsp;&nbsp;&nbsp;&nbsp;puts "tee hee!"  
&nbsp;&nbsp;end

&nbsp;&nbsp;def self.included(receiver)  
&nbsp;&nbsp;&nbsp;&nbsp;receiver.extend ClassMethods  
&nbsp;&nbsp;end  
end

class Shape  
&nbsp;&nbsp;include Color  
&nbsp;&nbsp;initialize\_colors  
&nbsp;&nbsp;#=\> "woot!"  
end

Shape.new.giggle  
#=\> "tee hee!"

---

<div class="post-metadata">

**Author:** ![Greg\_Willits](https://avatars.discourse-cdn.com/v4/letter/g/cab0a1/32.png) [@Greg\_Willits](https://rubytalk.org/u/Greg_Willits)\
**Post date:** [22 November 2007 18:57 UTC](https://rubytalk.org/t/class-methods-from-modules/42428/4 "2007-11-22T18:57:38Z")

</div>

Eric I. wrote:

> Hi Greg,
> 
> Well the methods you can call \*as\* a class is being defined are  
> \*class\* methods. initialize\_colors is an instance method, as it  
> should be since it's setting instance variables.
> 
> It seems to me that you want to call initialize\_colors not as the  
> class is being defined but as instances are being created. And  
> therefore the call to initialize\_colors should be inside Square's  
> initialize method.

The exercise is to figure how to make initialize\_colors work as a class  
method -- so if the instance vars are misleading, then it's those I  
would change.

I am just experiementing to understand with working examples how various  
syntax and behavior are related in Ruby to acheive various levels of  
functionality at the class, instance, and module realms.

Another way to look at this is that I'm trying to understand how some  
things in Rails work by replicating them on a micro scale -- in this  
case a good example might be how the validates\_ methods all work.  
They're called as class methods from within a subclass and I would  
expect they're implemented via modules. I've traced the source, but  
there's too much noise for me at this stage of my Ruby experience.

-- gw

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

---

<div class="post-metadata">

**Author:** ![Greg\_Willits](https://avatars.discourse-cdn.com/v4/letter/g/cab0a1/32.png) [@Greg\_Willits](https://rubytalk.org/u/Greg_Willits)\
**Post date:** [23 November 2007 03:46 UTC](https://rubytalk.org/t/class-methods-from-modules/42428/5 "2007-11-23T03:46:13Z")

</div>

Gavin Kistner wrote:

> Greg Willits wrote:
> 
> > In this play code below, I want to use initialize\_colors in either the  
> > class Shape or Square as a class method.
> 
> Including a module in a class causes instance methods of the module to  
> be in the lookup path for instances of the class. Extending a class  
> using a module causes instance methods of the module to be in the  
> lookup path for the class itself.
> 
> (See [http://phrogz.net/RubyLibs/RubyMethodLookupFlow.png\](http://phrogz.net/RubyLibs/RubyMethodLookupFlow.png%5C))
> 
> A common idiom to be able to cause a class to get both class and  
> instance methods when including a single module follows:

Cool, thanks. But...

In this code the fill\_color is still never set to black. It seems to me  
that Module code even if added as class methods cannot access class  
variables??

-- gw

module Color  
&nbsp;&nbsp;module ClassMethods  
&nbsp;&nbsp;&nbsp;&nbsp;def initialize\_colors  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@fill\_color = 'black'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p "woot!"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;def self.included(receiver)  
&nbsp;&nbsp;&nbsp;&nbsp;receiver.extend ClassMethods  
&nbsp;&nbsp;end  
end

class Shape  
&nbsp;&nbsp;include Color  
&nbsp;&nbsp;@@fill\_color = 'white'  
&nbsp;&nbsp;initialize\_colors

&nbsp;&nbsp;def self.fill\_color  
&nbsp;&nbsp;&nbsp;&nbsp;@@fill\_color  
&nbsp;&nbsp;end  
end

Shape.new  
p Shape.fill\_color

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

---

<div class="post-metadata">

**Author:** ![Greg\_Willits](https://avatars.discourse-cdn.com/v4/letter/g/cab0a1/32.png) [@Greg\_Willits](https://rubytalk.org/u/Greg_Willits)\
**Post date:** [22 November 2007 19:46 UTC](https://rubytalk.org/t/class-methods-from-modules/42428/6 "2007-11-22T19:46:18Z")

</div>

I found class \<\< self, but still here's an even simpler example that  
doesn't work as I thought it would:

module Color  
&nbsp;&nbsp;def set\_fill(color)  
&nbsp;&nbsp;&nbsp;&nbsp;puts "testing"  
&nbsp;&nbsp;&nbsp;&nbsp;@@fill\_color = color  
&nbsp;&nbsp;end  
end

class Shape

&nbsp;&nbsp;@@fill\_color = 'black'

&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;include Color  
&nbsp;&nbsp;end

&nbsp;&nbsp;def self.fill\_color  
&nbsp;&nbsp;&nbsp;&nbsp;@@fill\_color  
&nbsp;&nbsp;end

end

Shape.new  
puts Shape.fill\_color

Shape.set\_fill('red')  
puts Shape.fill\_color

I get:

black  
testing  
black

--gw

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

---

<div class="post-metadata">

**Author:** ![Greg\_Willits](https://avatars.discourse-cdn.com/v4/letter/g/cab0a1/32.png) [@Greg\_Willits](https://rubytalk.org/u/Greg_Willits)\
**Post date:** [23 November 2007 04:54 UTC](https://rubytalk.org/t/class-methods-from-modules/42428/7 "2007-11-23T04:54:42Z")

</div>

Greg Willits wrote:

> Gavin Kistner wrote:
> 
> > Greg Willits wrote:
> > 
> > > In this play code below, I want to use initialize\_colors in either the  
> > > class Shape or Square as a class method.
> > 
> > Including a module in a class causes instance methods of the module to  
> > be in the lookup path for instances of the class. Extending a class  
> > using a module causes instance methods of the module to be in the  
> > lookup path for the class itself.
> > 
> > (See [http://phrogz.net/RubyLibs/RubyMethodLookupFlow.png\](http://phrogz.net/RubyLibs/RubyMethodLookupFlow.png%5C))
> > 
> > A common idiom to be able to cause a class to get both class and  
> > instance methods when including a single module follows:
> 
> Cool, thanks. But...
> 
> In this code the fill\_color is still never set to black. It seems to me  
> that Module code even if added as class methods cannot access class  
> variables?

OK, nevermind -- I get it now.

even though they have the same name they are actually two independent  
variables in the two separate "name spaces/realms" of the model & class  
containers.

I thought the "inclusion" would fuse the two spaces into one.

-- gw

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