does module have instance_methods?

R

Ruby Newbee

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.
 
G

Gary Wright

I'm still confused, since module can't be instantiated, why it has the
instance methods?
=20
irb(main):162:0> module Mymod
irb(main):163:1> def myway;end
irb(main):164:1> end
=3D> nil
irb(main):165:0> Mymod.respond_to? :myway
=3D> false
irb(main):166:0> Mymod.instance_methods
=3D> [: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
 
J

Jesús Gabriel y Galán

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> =A0 def myway;end
irb(main):164:1> end
=3D> nil
irb(main):165:0> Mymod.respond_to? :myway
=3D> false
irb(main):166:0> Mymod.instance_methods
=3D> [: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
=3D> nil
irb(main):006:0> a =3D A.new
=3D> #<A:0xb7d5e7bc>
irb(main):008:0> a.class.ancestors
=3D> [A, Object, Kernel]
irb(main):009:0> a.test
=3D> "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
=3D> nil
irb(main):017:0> class A
irb(main):018:1> include Test
irb(main):019:1> end
=3D> A
irb(main):020:0> a.class.ancestors
=3D> [A, Test, Object, Kernel]
irb(main):011:0> a.test
=3D> "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
=3D> 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
=3D> nil
irb(main):011:0> a =3D A.new
=3D> #<A:0xb7cdddc4>
irb(main):012:0> a.extend Test
=3D> #<A:0xb7cdddc4>
irb(main):013:0> class << a; self; end.ancestors
=3D> [Test, A, Object, Kernel]
irb(main):014:0> a.test
=3D> "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.
 
R

Ruby Newbee

2009/12/15 Jes=C3=BAs Gabriel y Gal=C3=A1n said:
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> =C2=A0 def myway;end
irb(main):164:1> end
=3D> nil
irb(main):165:0> Mymod.respond_to? :myway
=3D> false
irb(main):166:0> Mymod.instance_methods
=3D> [: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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top