Difference between rb_define_module_function and rb_define_singleton_method?

J

John Lam

------=_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--
 
T

ts

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
 
J

John Lam

------=_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--
 
R

Ryan Leavengood

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
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top