From: jeremy@... (Jeremy Henty) Date: 2001-10-08T05:23:17+09:00 Subject: [ruby-talk:22211] Re: Why does Class undefine module_function? Matz writes: > ... Jeremy Henty writes: > > |/usr/local/src/ruby-1.6.5/eval.c: line 5961 > | > | rb_undef_method(rb_cClass, "module_function"); > | > | ... I > |see no reason why a class should not invoke module_function just as a > |module can. What am I missing? > > Remove that line, recompile, then try the following. > > class String > module_function :sub > end > String.sub(/a/, "b") > > It would crash the interpreter. So the reason is that instance methods of a class may depend on the receiver being of the right class (particularly if they are builtins or extensions that access the underlying C structures) whereas instance methods of a module are agnostic about the class of the receiver (because the module could be included into any class) and so can be invoked on any object without fear? Including as a special case the module itself? Thanks for your help. Cheers, Jeremy Henty -- http://www.rubygarden.com/ruby?JeremyHenty Ruby: programmer's best friend -- John Robson