# Class methods in modules

**URL:** <https://www.ruby-forum.com/t/class-methods-in-modules/137841>\
**Category:** Ruby\
**Created:** [May 16, 2008, 8:56pm UTC](https://www.ruby-forum.com/t/class-methods-in-modules/137841 "2008-05-16T20:56:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [May 16, 2008, 8:56pm UTC](https://www.ruby-forum.com/t/class-methods-in-modules/137841/1 "2008-05-16T20:56:43Z")

</div>

Hi

This maybe a pretty obvious question - but I decided to ask anyway.

I am trying to class a method defined within an included module from a  
class level method as below.

Is there a better way to do this? (Assumming that the class level  
method cannot be changed to a instance method.)

class Report  
include FormatHelper

self.generate\_report  
…  
my\_date = self.format\_date(…)  
…  
end  
end

module FormatHelper

self.format\_date(date)  
…  
end  
end

Any help welcome

Cheers  
Adrian

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [May 16, 2008, 9:37pm UTC](https://www.ruby-forum.com/t/class-methods-in-modules/137841/2 "2008-05-16T21:37:52Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On May 16, 2008, at 8:55 PM, Adrian wrote:

> class Report
> 
> self.format\_date(date)  
> …  
> end  
> end
> 
> Any help welcome
> 
> Cheers  
> Adrian

Hi,

Class Methods of Modules are local to the module.

If you want to extend a Class by the instance Methods of a Module, you  
have to possibilities:

Use “extend” instead of “include”:

module A  
def foo  
“foo”  
end  
end

class C  
extend A

def self.bar  
foo  
end  
end

include the Methods within the “metaclass”:

class D  
class \<\< self  
include A  
end

def self.bar  
foo  
end  
end

If you wish to keep class an instance methods separated, it is  
somewhat popular  
to define them in different modules:

module FormatHelper  
module ClassMethods  
def foo

##### 

```
  end

  #some more methods

```

end

module InstanceMethods  
def foo

##### 

```
  end

  #some more methods

```

end  
end

class D  
include FormatHelper::InstanceMethods  
extend FormatHelper::ClassMethods  
end

Have a lot of fun.  
Florian G.  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkgt4kgACgkQJA/zY0IIRZahUACfSmUEdASjoZ+6DvQO372+VrHN  
27UAnRraICcKvGegxzRb5xWeqargnoKs  
=zIpa  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [May 17, 2008, 12:36am UTC](https://www.ruby-forum.com/t/class-methods-in-modules/137841/3 "2008-05-17T00:36:22Z")

</div>

On Fri, May 16, 2008 at 3:36 PM, Florian G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> #some more methods
> 
> ```
> 
> class D  
> include FormatHelper::InstanceMethods  
> extend FormatHelper::ClassMethods  
> end

Or for a module, which adds both class and instance methods, the  
popular way is something like this:

module FormatHelper

```
 def self.included(class_or_module)
       class_or_module.extend(ClassMethods)
 end

 module ClassMethods
     ... class methods go here
 end

 .. instance methods go here

```

end

Then when a class includes the module it gets both sets of methods.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [May 18, 2008, 12:40am UTC](https://www.ruby-forum.com/t/class-methods-in-modules/137841/4 "2008-05-18T00:40:05Z")

</div>

Interesting - so class methods in modules don’t make any sense at all?  
Calling the modules class methods directly and special class methods  
such as included(base) and extended(base) are the exception of course.

Would you agree with that statement (question to everyone)?

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [May 18, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/class-methods-in-modules/137841/5 "2008-05-18T16:10:08Z")

</div>

On Sat, May 17, 2008 at 6:39 PM, Christoph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Interesting - so class methods in modules don’t make any sense at all?  
> Calling the modules class methods directly and special class methods such as  
> included(base) and extended(base) are the exception of course.
> 
> Would you agree with that statement (question to everyone)?

Well, since Modules aren’t classes, they can’t have class methods per  
se. They can have module methods:

module Mod  
def self.i\_am\_a\_module\_method  
end  
end

Modules are objects themselves so can have methods.

For an example of module methods in the standard library, look at the  
Math module. For example to get the natural log of 10 you can write:

Math.log(10)

If you simply write:

log(10)

you’ll get a method not found error. But you could also include Math  
into a class or module and then any methods of that class or module,  
could use the simpler call.

Ruby has a method in Module#module\_function which takes one or more  
method names of module instance methods and copies them as module  
methods. The Math module effectively does just that to make it’s  
methods available either as methods of the Math object, or as instance  
methods when the Math module is included.

As an interesting sidenote, although Class is a subclass of Module,  
you can’t use module\_function in a class to copy instance methods to  
class methods, the MRI implementation undefines the method in Class.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
