I'm working with an extension module (written in C) that is a wrapper
of another C library. The extension module makes one big Ruby module
with a bunch of module methods. That is, the calls in the extension
module C code are all rb_define_module_function(...).
If I build and 'require' this module, I can see
irb:> TheModule.instance_methods
==> []
which is expected.
I do:
irb:> TheModule.methods - Object.methods
and get all the ones I expect to see. Ok.
What's confusing is, in my Ruby code, when I use this module, I'm able
to do this:
require 'the_module'
include TheModule
and then call the supposed module methods in TheModule without
prefixing them with "TheModule.". That is, I can call "foo123" instead
of "TheModule.foo123" and it works.
Am I not understanding correctly how "include" works? Can anyone tell
me why this works? I tried it with some simple example code (pure
Ruby):
----------------- code -------------------
module TheModule
def self.foo123
puts "Doing stuff..."
end
end
include TheModule
foo123
----------------- /code -------------------
but it fails, as expected (seemingly in conflict with how the
extension module is behaving). BTW, if I try:
----------------- code -------------------
module TheModule
def self.foo123
puts "Doing stuff..."
end
end
TheModule.foo123
p TheModule.methods - Object.methods
----------------- /code -------------------
I get:
$ ./foo.rb
Doing stuff...
["foo123"]
as expected.
Looking at README.EXT, it has this to say on the matter:
------------------------------- 8< --------------------------------
The other [way to define methods] is to define module functions, which
are private AND singleton
methods of the module. For example, sqrt is the module function
defined in Math module. It can be call in the form like:
Math.sqrt(4)
or
include Math
sqrt(4)
To define module functions, use:
void rb_define_module_function(VALUE module, const char *name,
VALUE (*func)(), int argc)
------------------------------- 8< --------------------------------
I don't understand why it says that they're private -- I'm calling
them from my Ruby code and it seems they're being accessed, so they
don't seem private... What does the author of the README mean by
"private" here?
Thanks for any insights.
---John