# Class methods in a module?

**URL:** <https://www.ruby-forum.com/t/class-methods-in-a-module/168769>\
**Category:** Ruby\
**Created:** [May 25, 2009, 5:07pm UTC](https://www.ruby-forum.com/t/class-methods-in-a-module/168769 "2009-05-25T17:07:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [May 25, 2009, 5:07pm UTC](https://www.ruby-forum.com/t/class-methods-in-a-module/168769/1 "2009-05-25T17:07:43Z")

</div>

At Scotland On Rails a few months ago, I was shown a nice way to put  
class methods in a module, alongside instance methods, in such a way as  
they will get added as class methods automatically, without any work  
required on the part of the person including the module.

Unfortunately i failed to make a note of how to do this and i’ve since  
forgotten. Can anyone show me?

It was something along these lines…(the following doesn’t work btw)

module Mappable

#instance methods  
def foo  
“foo”  
end

#class methods  
class \<\< self.class  
def bar  
“bar”  
end  
end

end

can anyone set me straight?  
thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [May 25, 2009, 5:52pm UTC](https://www.ruby-forum.com/t/class-methods-in-a-module/168769/2 "2009-05-25T17:52:27Z")

</div>

2009/5/25 Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> At Scotland On Rails a few months ago, I was shown a nice way to put  
> class methods in a module, alongside instance methods, in such a way as  
> they will get added as class methods automatically, without any work  
> required on the part of the person including the module.
> 
> Unfortunately i failed to make a note of how to do this and i’ve since  
> forgotten. Can anyone show me?

I’m not sure if you can pick up the singleton methods from the module  
and  
automatically copy them to the class – I’ve got type errors before when  
trying to move and rebind methods. I did come up with this, though,  
which is  
more expressive than writing an included() hook on every module:

> <https://gist.github.com/jcoglan/114042>

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [May 25, 2009, 5:53pm UTC](https://www.ruby-forum.com/t/class-methods-in-a-module/168769/3 "2009-05-25T17:53:38Z")

</div>

On Mon, May 25, 2009 at 11:07 AM, Max W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> module Mappable  
> end  
> end
> 
> end
> 
> can anyone set me straight?  
> thanks  
> max

The usual way is to use the hook method Module#included, something like:

module Mappable

#instance methods  
def foo  
“foo”  
end

#class methods  
module ClassMethods  
def bar  
“bar”  
end  
end

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

## By the way this nesting of a Module inside a module is another variant on something discussed in another recent ruby-talk thread.

Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [May 25, 2009, 9:08pm UTC](https://www.ruby-forum.com/t/class-methods-in-a-module/168769/4 "2009-05-25T21:08:33Z")

</div>

Max W. wrote:

> At Scotland On Rails a few months ago, I was shown a nice way to put  
> class methods in a module, alongside instance methods, in such a way as  
> they will get added as class methods automatically, without any work  
> required on the part of the person including the module.
> 
> Unfortunately i failed to make a note of how to do this and i’ve since  
> forgotten. Can anyone show me?
> 
> It was something along these lines…(the following doesn’t work btw)

module Stuff  
module ClassMethods  
def biff(\*args)  
# …  
end

```
 def baz(*args)
   # ...
 end

```

end

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

end

The methods defined in Stuff::ClassMethods are added as class methods  
when

include Stuff

is called

–  
James B.

[www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
[www.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
[www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
[www.neurogami.com](http://www.neurogami.com) - Smart application development
