does module have instance_methods?

Discussion in 'Ruby' started by Ruby Newbee, Dec 15, 2009.

  1. Ruby Newbee

    Ruby Newbee Guest

    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.
    Ruby Newbee, Dec 15, 2009
    #1
    1. Advertising

  2. Ruby Newbee

    Gary Wright Guest

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

    > 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
    Gary Wright, Dec 15, 2009
    #2
    1. Advertising

  3. On Tue, Dec 15, 2009 at 5:25 AM, Ruby Newbee <> wrote:
    > 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.
    Jesús Gabriel y Galán, Dec 15, 2009
    #3
  4. Ruby Newbee

    Ruby Newbee Guest

    2009/12/15 Jes=C3=BAs Gabriel y Gal=C3=A1n <>:
    > On Tue, Dec 15, 2009 at 5:25 AM, Ruby Newbee <> wrote=

    :
    >> 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.
    Ruby Newbee, Dec 15, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=

    Does a resource have to have all phrases in it?

    =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=, Jan 30, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    369
    Neil Ni
    Feb 3, 2006
  2. GGarramuno

    proc question and instance_methods

    GGarramuno, Jan 6, 2004, in forum: Ruby
    Replies:
    6
    Views:
    91
  3. Marcin Mielżyński

    builtin singleton & instance_methods

    Marcin Mielżyński, Nov 24, 2006, in forum: Ruby
    Replies:
    0
    Views:
    73
    Marcin Mielżyński
    Nov 24, 2006
  4. Trans
    Replies:
    1
    Views:
    85
    James Coglan
    Aug 24, 2008
  5. Xavier Noria
    Replies:
    1
    Views:
    71
    Xavier Noria
    Mar 19, 2010
Loading...

Share This Page