On 7/5/06, ts <decoux / moulon.inra.fr> wrote:
> >>>>> "J" == John Gabriele <jmg3000 / gmail.com> writes:
>
> J> Am I not understanding correctly how "include" works? Can anyone tell
> J> me why this works? I tried it with some simple example code (pure
> J> Ruby):
>
>  Well try this
>
> moulon% cat b.rb
> #!/usr/bin/ruby
>
> module TheModule
>    def self.foo123
>       puts "Doing stuff..."
>    end
>
>    private
>    def foo123
>       puts "I'm the private method doing stuff..."
>    end
> end
>
> include TheModule
> TheModule.foo123
> foo123
> self.foo123 # will give an error because it's a private method
> moulon%
>
> moulon% ./b.rb
> Doing stuff...
> I'm the private method doing stuff...
> ./b.rb:17: private method `foo123' called for main:Object (NoMethodError)
> moulon%
>
>
>  module_function define a public singleton method and a private method.

Thanks for the reply Guy.

So, rb_define_module_function does *both*? Ah. Interesting.

Now, I've always read that a "singleton method" is one that you add to
your own instance of something -- such that, other instances don't
have the method; just yours. In the code above, I see a public module
method... why do you refer to it as a public "singleton" method?

Also, I see that the reason we can call foo123 like that (after doing
the include) is because it's an instance method and we're getting it
when we do the include. It may be private, but since we've included
the module, it's like it's a private method in our own class (which we
can of course access). The difference between calling 'foo123' and
'self.foo123' seems more subtle... possibly having to do with how the
mixin feature is implemented? (the PickAxe 2nd ed. mentions an
"anonymous proxy class", p. 383)

Your comment "# will give an error because it's a private method"
doesn't make sense to me, since just calling 'foo123' does in fact
work, and foo123 is private.

---John