Difference between rb_define_module_function and rb_define_singleton_method?

Discussion in 'Ruby' started by John Lam, Dec 7, 2005.

  1. John Lam

    John Lam Guest

    ------=_Part_2155_29077248.1133976099313
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    Is there a difference between calling rb_define_module_function and
    rb_define_singleton_method on a module?

    Thanks
    -John
    http://www.iunknown.com

    ------=_Part_2155_29077248.1133976099313--
     
    John Lam, Dec 7, 2005
    #1
    1. Advertising

  2. John Lam

    ts Guest

    >>>>> "J" == John Lam <> writes:

    J> Is there a difference between calling rb_define_module_function and
    J> rb_define_singleton_method on a module?

    rb_define_module_function() make the 2 calls

    rb_define_private_method()
    rb_define_singleton_method()


    Guy Decoux
     
    ts, Dec 7, 2005
    #2
    1. Advertising

  3. John Lam

    John Lam Guest

    ------=_Part_2795_24507389.1133977749572
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    Thanks!


    rb_define_module_function() make the 2 calls
    >
    > rb_define_private_method()
    > rb_define_singleton_method()
    >
    >
    > Guy Decoux
    >
    >


    ------=_Part_2795_24507389.1133977749572--
     
    John Lam, Dec 7, 2005
    #3
  4. On 12/7/05, ts <> wrote:
    >
    > rb_define_module_function() make the 2 calls
    >
    > rb_define_private_method()
    > rb_define_singleton_method()


    And this allows the given method to be called with both the module as
    a receiver, as well as directly when the module is included in a
    class. For example, the methods in module Math are defined this way:

    irb(main):001:0> class MyMath
    irb(main):002:1> include Math
    irb(main):003:1> def some_complicated_math
    irb(main):004:2> sqrt(4)
    irb(main):005:2> end
    irb(main):006:1> end
    =3D> nil
    irb(main):007:0> MyMath.new.some_complicated_math
    =3D> 2.0

    If they aren't defined this way, you can't call them from an instance:

    irb(main):008:0> module Test
    irb(main):009:1> def self.mod_meth
    irb(main):010:2> p 'mod_meth'
    irb(main):011:2> end
    irb(main):012:1> end
    =3D> nil
    irb(main):013:0> class MyTest
    irb(main):014:1> include Test
    irb(main):015:1> def meth
    irb(main):016:2> mod_meth
    irb(main):017:2> end
    irb(main):018:1> end
    =3D> nil
    irb(main):019:0> MyTest.new.meth
    NameError: undefined local variable or method `mod_meth' for #<MyTest:0x2b3=
    88d8>
    from (irb):16:in `meth'
    from (irb):19

    In Ruby you can get the equivalent behavior by defining instance
    methods in the module, then extend self at the end:

    irb(main):020:0> module Test2
    irb(main):021:1> def mod_meth2
    irb(main):022:2> p 'mod_meth2'
    irb(main):023:2> end
    irb(main):024:1> extend self
    irb(main):025:1> end
    =3D> Test2
    irb(main):026:0> Test2.mod_meth2
    "mod_meth2"
    =3D> nil
    irb(main):027:0> class MyTest2
    irb(main):028:1> include Test2
    irb(main):029:1> def meth2
    irb(main):030:2> mod_meth2
    irb(main):031:2> end
    irb(main):032:1> end
    =3D> nil
    irb(main):033:0> MyTest2.new.meth2
    "mod_meth2"

    Regards,
    Ryan
     
    Ryan Leavengood, Dec 7, 2005
    #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. jakk
    Replies:
    4
    Views:
    12,511
  2. Santa
    Replies:
    1
    Views:
    1,140
    Mark A. Odell
    Jul 17, 2003
  3. Replies:
    2
    Views:
    317
  4. Suraj Kurapati
    Replies:
    0
    Views:
    130
    Suraj Kurapati
    Feb 17, 2006
  5. Suraj Kurapati
    Replies:
    3
    Views:
    120
    Suraj Kurapati
    Feb 19, 2006
Loading...

Share This Page