# Does module have instance\_methods?

**URL:** <https://www.ruby-forum.com/t/does-module-have-instance-methods/180224>\
**Category:** Ruby\
**Created:** [December 15, 2009, 5:27am UTC](https://www.ruby-forum.com/t/does-module-have-instance-methods/180224 "2009-12-15T05:27:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 15, 2009, 5:27am UTC](https://www.ruby-forum.com/t/does-module-have-instance-methods/180224/1 "2009-12-15T05:27:41Z")

</div>

I’m still confused, since module can’t be instantiated, why it has the  
instance methods?

irb(main):162:0\> module Mymod  
irb(main):163:1\> def myway;end  
irb(main):164:1\> end  
=\> nil  
irb(main):165:0\> Mymod.respond\_to? :myway  
=\> false  
irb(main):166:0\> Mymod.instance\_methods  
=\> [:myway]

Thanks.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 15, 2009, 5:55am UTC](https://www.ruby-forum.com/t/does-module-have-instance-methods/180224/2 "2009-12-15T05:55:11Z")

</div>

On Dec 14, 2009, at 11:25 PM, Ruby N. wrote:

> =\> [:myway]  
> You can think of a module as a container for method definitions. By  
> storing the definitions in a module you can reuse the methods without  
> having to duplicate code by ‘including’ the module in a class or even  
> another module:

module A  
def a1; “method a1”; end  
def a2; “method a2”; end  
end

class B  
include A  
end

B.new.a1  
B.new.a2

class C  
include A  
end

C.new.a1  
C.new.a2

# You can even next modules within modules:

module D  
include A  
end

class E  
include D  
end

E.new.a1

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 15, 2009, 10:22am UTC](https://www.ruby-forum.com/t/does-module-have-instance-methods/180224/3 "2009-12-15T10:22:39Z")

</div>

On Tue, Dec 15, 2009 at 5:25 AM, Ruby N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> =\> [:myway]  
> It has instance\_methods because those methods will be called on an  
> instance. It will not be an instance of that module, but it will be an  
> object (an instance of some class). They are very similar to the  
> instance\_methods in a class, in the sense that an object at some point  
> will have this module or class in the lookup path when you call a  
> method on that instance, so that’s the way an instance\_method can be  
> called. Look:

irb(main):001:0\> class A  
irb(main):002:1\> def test  
irb(main):003:2\> “test”  
irb(main):004:2\> end  
irb(main):005:1\> end  
=\> nil  
irb(main):006:0\> a = A.new  
=\> #\<A:0xb7d5e7bc\>  
irb(main):008:0\> a.class.ancestors  
=\> [A, Object, Kernel]  
irb(main):009:0\> a.test  
=\> “test”

We can say a.test because A is in the lookup path of a’s methods. The  
same happens with a module:

irb(main):012:0\> module Test  
irb(main):013:1\> def test  
irb(main):014:2\> “the module test”  
irb(main):015:2\> end  
irb(main):016:1\> end  
=\> nil  
irb(main):017:0\> class A  
irb(main):018:1\> include Test  
irb(main):019:1\> end  
=\> A  
irb(main):020:0\> a.class.ancestors  
=\> [A, Test, Object, Kernel]  
irb(main):011:0\> a.test  
=\> “Test’s test”

As you can see, when class A includes the module, it appears in the  
lookup path of A, so its instances can call Test’s instance methods.  
Let’s do it in a different way now:

irb(main):001:0\> module Test  
irb(main):002:1\> def test  
irb(main):003:2\> “Test’s test”  
irb(main):004:2\> end  
irb(main):005:1\> end  
=\> nil  
irb(main):006:0\> class A  
irb(main):007:1\> def test  
irb(main):008:2\> “A’s test”  
irb(main):009:2\> end  
irb(main):010:1\> end  
=\> nil  
irb(main):011:0\> a = A.new  
=\> #\<A:0xb7cdddc4\>  
irb(main):012:0\> a.extend Test  
=\> #\<A:0xb7cdddc4\>  
irb(main):013:0\> class \<\< a; self; end.ancestors  
=\> [Test, A, Object, Kernel]  
irb(main):014:0\> a.test  
=\> “Test’s test”

Instead of the class including the module, we extend just that  
instance with the module. If we check the lookup path of the singleton  
class of a, we can see that the module goes before the class, so in  
this case the module’s method is called before the class one.

Jesus.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 15, 2009, 11:03am UTC](https://www.ruby-forum.com/t/does-module-have-instance-methods/180224/4 "2009-12-15T11:03:03Z")

</div>

2009/12/15 JesÃºs Gabriel y GalÃ¡n [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > irb(main):166:0\> Mymod.instance\_methods  
> > =\> [:myway]
> 
> It has instance\_methods because those methods will be called on an  
> instance. It will not be an instance of that module, but it will be an  
> object (an instance of some class). They are very similar to the  
> instance\_methods in a class, in the sense that an object at some point  
> will have this module or class in the lookup path when you call a  
> method on that instance, so that’s the way an instance\_method can be  
> called. Look:

Thanks Jesus, that has make things be clear.
