Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Difference between rb_define_module_function and rb_define_singleton_method?

Reply
Thread Tools

Difference between rb_define_module_function and rb_define_singleton_method?

 
 
John Lam
Guest
Posts: n/a
 
      12-07-2005
------=_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--


 
Reply With Quote
 
 
 
 
ts
Guest
Posts: n/a
 
      12-07-2005
>>>>> "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


 
Reply With Quote
 
 
 
 
John Lam
Guest
Posts: n/a
 
      12-07-2005
------=_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--


 
Reply With Quote
 
Ryan Leavengood
Guest
Posts: n/a
 
      12-07-2005
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 01-06-2011 05:00 PM
SystemStackError when rb_define_module_function() * 3 Suraj Kurapati Ruby 3 02-19-2006 07:18 PM
SystemStackError when rb_define_module_function() * 3 Suraj Kurapati Ruby 0 02-17-2006 06:20 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57