# Class Methods from Modules

**URL:** <https://www.ruby-forum.com/t/class-methods-from-modules/121019>\
**Category:** Ruby\
**Created:** [November 22, 2007, 6:58pm UTC](https://www.ruby-forum.com/t/class-methods-from-modules/121019 "2007-11-22T18:58:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 22, 2007, 6:58pm UTC](https://www.ruby-forum.com/t/class-methods-from-modules/121019/1 "2007-11-22T18:58:22Z")

</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

def initialize\_colors  
@fill\_color = ‘ffffff’  
@border\_color = ‘000000’  
end

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

class Shape

include Color

attr\_reader :fill\_color, :border\_color

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

class Square \< Shape

initialize\_colors

end

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

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 22, 2007, 7:41pm UTC](https://www.ruby-forum.com/t/class-methods-from-modules/121019/2 "2007-11-22T19:41:39Z")

</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)

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 22, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/class-methods-from-modules/121019/3 "2007-11-22T19:57:41Z")

</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

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 22, 2007, 8:46pm UTC](https://www.ruby-forum.com/t/class-methods-from-modules/121019/4 "2007-11-22T20:46:22Z")

</div>

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

module Color  
def set\_fill(color)  
puts “testing”  
@@fill\_color = color  
end  
end

class Shape

@@fill\_color = ‘black’

class \<\< self  
include Color  
end

def self.fill\_color  
@@fill\_color  
end

end

Shape.new  
puts Shape.fill\_color

Shape.set\_fill(‘red’)  
puts Shape.fill\_color

I get:

black  
testing  
black

–gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 23, 2007, 4:21am UTC](https://www.ruby-forum.com/t/class-methods-from-modules/121019/5 "2007-11-23T04:21:16Z")

</div>

Greg W. 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))

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  
module ClassMethods  
def initialize\_colors  
p “woot!”  
end  
end

def giggle  
puts “tee hee!”  
end

def self.included(receiver)  
receiver.extend ClassMethods  
end  
end

class Shape  
include Color  
initialize\_colors  
#=\> “woot!”  
end

Shape.new.giggle  
#=\> “tee hee!”

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 23, 2007, 4:46am UTC](https://www.ruby-forum.com/t/class-methods-from-modules/121019/6 "2007-11-23T04:46:14Z")

</div>

Gavin K. wrote:

> Greg W. 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))
> 
> 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  
module ClassMethods  
def initialize\_colors  
@@fill\_color = ‘black’  
p “woot!”  
end  
end

def self.included(receiver)  
receiver.extend ClassMethods  
end  
end

class Shape  
include Color  
@@fill\_color = ‘white’  
initialize\_colors

def self.fill\_color  
@@fill\_color  
end  
end

Shape.new  
p Shape.fill\_color

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 23, 2007, 5:54am UTC](https://www.ruby-forum.com/t/class-methods-from-modules/121019/7 "2007-11-23T05:54:45Z")

</div>

Greg W. wrote:

> Gavin K. wrote:
> 
> > Greg W. 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))
> > 
> > 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
