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