# Modules and class methods

**URL:** <https://www.ruby-forum.com/t/modules-and-class-methods/103346>\
**Category:** Ruby\
**Created:** [May 4, 2007, 4:00pm UTC](https://www.ruby-forum.com/t/modules-and-class-methods/103346 "2007-05-04T16:00:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bpauly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecd19e/32.png) [@bpauly](https://www.ruby-forum.com/u/bpauly)\
**Post date:** [May 4, 2007, 4:00pm UTC](https://www.ruby-forum.com/t/modules-and-class-methods/103346/1 "2007-05-04T16:00:39Z")

</div>

Why does this not work!

########################################  
module Foo  
def self.foo a  
puts a  
end  
end

class Bar  
include Foo  
foo “hello”  
end

foo.rb:20: undefined method `foo’ for Bar:Class (NoMethodError)  
####################################

but this does

class Bar  
def self.foo a  
puts a  
end  
foo “hello”  
end

What am I misunderstanding about modules???

---

<div class="post-metadata">

**Author:** ![bpauly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecd19e/32.png) [@bpauly](https://www.ruby-forum.com/u/bpauly)\
**Post date:** [May 4, 2007, 4:17pm UTC](https://www.ruby-forum.com/t/modules-and-class-methods/103346/2 "2007-05-04T16:17:47Z")

</div>

On 5/4/07, Brad P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Bar  
> def self.foo a  
> Brad  
> [http://xtargets.com](http://xtargets.com)
> 
> Module inclusion does not include singleton methods.

There are a few alternatives, the clearest one IMO being:

module Foo  
def foo a # note no self.  
puts a  
end  
end

class Bar  
extend Foo # note extend, \_not\_include  
foo “hello”  
end

The other option is to augment the include procedure. Ruby provides a  
hook  
Module#included.

module Foo  
def self.included(including\_class\_or\_module)  
class \<\< including\_class\_or\_module  
def foo a  
puts a  
end  
end  
end  
end

class Bar  
include Foo  
foo “hello”  
end

Usually when people do this and a module contains both singleton methods  
and  
instance methods it will look something like:

module Foo  
def inst\_meth1  
end

def inst\_meth1  
end

module ClassMethods  
def foo a  
puts a  
end  
end

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

class Bar  
include Foo  
foo “hello”  
end

Finally, you can of course use classes and plain old inheritance:

class Foo  
def self.foo a  
puts a  
end  
end

class Bar \< Foo  
foo “hello”  
end

---

<div class="post-metadata">

**Author:** ![bpauly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecd19e/32.png) [@bpauly](https://www.ruby-forum.com/u/bpauly)\
**Post date:** [September 25, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/modules-and-class-methods/103346/3 "2007-09-25T23:00:34Z")

</div>

> ```
> puts "here"
> 
> ```
> 
> David

Now that is the sneaky little hook I was looking for.

Thanks

Brad

---

<div class="post-metadata">

**Author:** ![bpauly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecd19e/32.png) [@bpauly](https://www.ruby-forum.com/u/bpauly)\
**Post date:** [September 25, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/modules-and-class-methods/103346/4 "2007-09-25T23:03:08Z")

</div>

Brad P. wrote:

> include Foo  
> puts a  
> [http://xtargets.com](http://xtargets.com)  
> The below code obviously work the way I wish

module Foo  
def foo  
puts “a”  
end  
end

class Bar  
extend Foo  
puts foo  
end

however is there any way to get the same effect by using include. I  
would like to mix in class and instance methods in one call. It seems  
I can do one or the other but not both at the same time. Is that  
correct?

---

<div class="post-metadata">

**Author:** ![bpauly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecd19e/32.png) [@bpauly](https://www.ruby-forum.com/u/bpauly)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/modules-and-class-methods/103346/5 "2007-09-25T23:04:58Z")

</div>

Hi –

On Fri, 4 May 2007, Brad P. wrote:

> > class Bar  
> > def self.foo a  
> > Brad  
> > class Bar  
> > extend Foo  
> > puts foo  
> > end
> 
> however is there any way to get the same effect by using include. I would  
> like to mix in class and instance methods in one call. It seems  
> I can do one or the other but not both at the same time. Is that  
> correct?

Basically, yes: mixing in a module inserts the module in only one  
method lookup path, and a class and its instances generally have very  
different lookup paths.

You can use the inherited hook in Module to do both – for example:

module M  
def self.included©  
m = self  
c.class\_eval { extend m }  
end

```
 def meth
   puts "here"
 end

```

end

class C  
include M  
end

C.meth # here  
C.new.meth # here

David
