# Modules

**URL:** <https://www.ruby-forum.com/t/modules/142858>\
**Category:** Ruby\
**Created:** [July 12, 2008, 7:10am UTC](https://www.ruby-forum.com/t/modules/142858 "2008-07-12T07:10:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [July 12, 2008, 7:10am UTC](https://www.ruby-forum.com/t/modules/142858/1 "2008-07-12T07:10:00Z")

</div>

I understand the way these things are working but I don’t really  
understand what’s going on and why.

module Foo  
def bar  
1  
end  
def Foo.zim  
2  
end  
end

> > Foo.zim # works, OK, fair enough  
> > 2
> 
> > Foo::zim # works, but why have two syntaxes that do the same?  
> > 2
> 
> > Foo.bar # NoMethodError. Shouldn’t this work though? What if I had a procedural API that I wanted to namespace: -

module SomeNamespace  
require ‘procedural\_api’  
end

SomeNamespace.function\_from\_procedural\_api # this ain’t gonna work!  
SomeNamespace::function\_from\_procedural\_api # nor is this

class SomeClass  
include Foo  
end

> > SomeClass.new.bar # OK, it’s been mixed in  
> > 1
> 
> > SomeClass.zim # NoMethodError, huh? Why doesn’t this work?  
> > SomeClass.new.zim # NoMethodError. So where has the zim definition actually gone? Doesn’t it get included at all?

I’m very confused by all this. Please help unravel the mess in my brain.

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [July 13, 2008, 2:51am UTC](https://www.ruby-forum.com/t/modules/142858/2 "2008-07-13T02:51:26Z")

</div>

Oliver S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> end
> 
> > > SomeClass.new.bar # OK, it’s been mixed in  
> > > 1  
> > > SomeClass.zim # NoMethodError, huh? Why doesn’t this work?  
> > > SomeClass.new.zim # NoMethodError.

> I’m very confused by all this. Please help unravel the mess in my brain.

I’d say you’re not confused at all. You’ve described perfectly how the  
language behaves.

m.

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [July 13, 2008, 3:47am UTC](https://www.ruby-forum.com/t/modules/142858/3 "2008-07-13T03:47:36Z")

</div>

> > > > Foo.zim # works, OK, fair enough  
> > > > 2
> > > 
> > > > Foo::zim # works, but why have two syntaxes that do the same?  
> > > > 2

objec.method works for any object. Everything is an object, so Foo is  
an object too.  
Class::method, Module::method works with class/module methods only.

> > > > SomeClass.zim # NoMethodError, huh? Why doesn’t this work?  
> > > > SomeClass.new.zim # NoMethodError.

Let’s take a different angle:

> > class Baz  
> > def Baz.qux # class method. you can look at it as a singleton method for Baz  
> > 3  
> > end  
> > end  
> > =\> nil

> > Baz.qux  
> > =\> 3
> 
> > Baz.new.qux # class method is not available to the instance  
> > NoMethodError: undefined method `qux’ for #Baz:0x35c6f4  
> > from (irb):7  
> > from :0

Thus zim exists only for the object of the Foo module - not any class  
that mixes in Foo, just for THE Foo itself.

Regards,  
Rimantas
