[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Difference between rb_define_module_function and rb_define_singleton_method?

John Lam

12/7/2005 5:22:00 PM

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

Thanks
-John
http://www.iu...
4 Answers

ts

12/7/2005 5:36:00 PM

0

>>>>> "J" == John Lam <drjflam@gmail.com> 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


John Lam

12/7/2005 5:49:00 PM

0

Thanks!


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

Ryan Leavengood

12/7/2005 5:51:00 PM

0

On 12/7/05, ts <decoux@moulon.inra.fr> 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
=> nil
irb(main):007:0> MyMath.new.some_complicated_math
=> 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
=> 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
=> nil
irb(main):019:0> MyTest.new.meth
NameError: undefined local variable or method `mod_meth' for #<MyTest:0x2b388d8>
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
=> Test2
irb(main):026:0> Test2.mod_meth2
"mod_meth2"
=> 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
=> nil
irb(main):033:0> MyTest2.new.meth2
"mod_meth2"

Regards,
Ryan


zzbunker@netscape.net

4/16/2009 10:41:00 PM

0

On Apr 16, 4:29 pm, AnAmericanCitizen <NoAmne...@earthlink.net> wrote:
> Disadvantaged students who were allowed the privilege of going to the same private
> school as the Obama girls will no longer have that opportunity.
>
> Another contingent thrown under the bus....AAC

Well, since the only thing most Teacher Unions even know
about School anymore is buses, anyway, the people who
can actually read, have already invented On-Line Publishing.
So, they mostly all can use their buses for they were meant for.
To give the The New York Times a Farewell Postcard.



>
>
>
> On Tue, 14 Apr 2009 16:38:09 -0700 (PDT), Betty Marino <bettymar...@gmail..com> wrote:
> >Current inner city students utilizing the voucher system read an
> >average of two grade levels  ahead of their piers, are %70 less likely
> >to be arrested, and cost the tax payers less per student.
>
> >Washington DC democrats voted to drop the program at the behest of the
> >teacher unions.  They all refused to give comment on the reasons for
> >dropping the program.
> >http://www.foxnews.com/politics/2009/04/14/dc-families-bemoan-i... Hide quoted text -
>
> - Show quoted text -