From: Trans Date: 2007-09-09T17:53:48+09:00 Subject: Re: module_function :func vs. MyModule.func? On Sep 8, 10:54 pm, 7stud -- wrote: > The following produce the same output: > > module MyModule > def greet > puts "hello" > end > > module_function :greet > end > > MyModule.greet > > #-----vs.----------- > > module MyModule > def MyModule.greet > puts "hello" > end > end > > MyModule.greet > > What is module_function used for? Allow me to further confuse you: module MyModule extend self def greet puts "hello" end end MyModule.greet Now I will explain. I the 1st case, using module_function makes an actual copy of the instance method and defines it as a singleton method of the module. In the 2nd case, you never have the instance method to begin with, you simply defined the singleton method directly. So to illustrate, the 1st example is equivalent to this: module MyModule def greet puts "hello" end def MyModule.greet puts "hello" end end Now for my last example. In which case, we defined the instance method, but have also included the module into it's own singleton space. It is functionally the same as the 1st case but is also dynamic --if we were to add a new method to MyModule later it would automatically appear as a module method as well. HTH, T.