Paul Brannan wrote:
> I have a function that I want to use as if it were a normal public
> instance method, but I don't always want to create an instance in order
> to use the method.  I thought module_function was the right way to do
> this.  But when I write:
> 
>   module Foo
>   public
>     def f1; end
>     def f2; end
>     module_function :f2
>     def f3; end
>   private
>     def f4; end
>   end
> 
> It makes sense that f4 is private, but it is not clear why f2 is
> private.
> 
> The workaround is obvious; I'm just interested in the reasoning behind
> module_function()'s behavior.

I'm gonna try to read Matz' mind for a second. :-)

When you call module_function, you end up with _two_ methods: Foo#f2 and
Foo.f2

Now, imagine the brain wracking as you try to debug someone who mixes
Foo#f2 calls with Foo.f2 calls (or Bar.f2 calls from a class Bar that
includes Foo). Why is this brainwracking? Well, remember those pesky
instance variables? Foo.f2 uses Foos instance variables, but Foo#f2
would mess with whatever object it is included into. Now go ahead and
use class variables and try to figure out what is going on when you mix
the modes of calling the method. (Go on, I dare ya!)

So it seems the semantics are _way_ different, and since it appears one
is using module_function as merely a neater way of writing def self.f2;
end, making Foo#f2 private protects you from shooting yourself in the
foot...

... while allowing you to blow it to bits with a public :f2 if you
absolutely want to. Enjoy the ECT while at the looney bin, if you choose
that path. 8-(

-- 
(\[ Kent Dahl ]/)_    _~_    __[ http://www.stud.ntnu.no/~kentda/ ]___/~
 ))\_student_/((  \__d L b__/  NTNU - graduate engineering - 5. year  )
( \__\_õüõß/__/ ) _)Industrial economics and technological management(
 \____/_öß\____/ (____engineering.discipline_=_Computer::Technology___)