From: Rick DeNatale Date: 2008-05-18T23:09:22+09:00 Subject: Re: Class methods in modules On Sat, May 17, 2008 at 6:39 PM, Christoph Schiessl wrote: > Interesting - so class methods in modules don't make any sense at all? > Calling the modules class methods directly and special class methods such as > included(base) and extended(base) are the exception of course. > > Would you agree with that statement (question to everyone)? Well, since Modules aren't classes, they can't have class methods per se. They can have module methods: module Mod def self.i_am_a_module_method end end Modules are objects themselves so can have methods. For an example of module methods in the standard library, look at the Math module. For example to get the natural log of 10 you can write: Math.log(10) If you simply write: log(10) you'll get a method not found error. But you could also include Math into a class or module and then any methods of that class or module, could use the simpler call. Ruby has a method in Module#module_function which takes one or more method names of module instance methods and copies them as module methods. The Math module effectively does just that to make it's methods available either as methods of the Math object, or as instance methods when the Math module is included. As an interesting sidenote, although Class is a subclass of Module, you can't use module_function in a class to copy instance methods to class methods, the MRI implementation undefines the method in Class. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/