From: "Brian Schröder" Date: 2005-03-04T20:22:10+09:00 Subject: Re: 'include ' and infinite recursion On Fri, 4 Mar 2005 19:25:21 +0900, Brian Candler wrote: > Hello, > > I am having trouble understanding what's going on here, and I wonder if any > guru here could enlighten me. I am getting infinite recursion when I don't > expect it, and it's something to do with 'including' a module at the top > level. > > [snip] > So my questions are: > > (1) Why is this method found? In other words, why does including MyApp at > the top level affect the methods which are visible within MyApp::O ? > When you are doing a top level include, you include into Object. Everything inherits from object, so every object has your function now. Thats the reason why methods like puts work, they are private methods of Object. > (2) (probably related). After doing 'include MyApp', I see that the > singleton class of MyApp::O includes MyApp in its ancestors. Why does it > appear there? I thought that the 'include' operation would only affect the > 'main' object. There is a chapter in pickaxe2 about classes and modules, maybe this helps? I don't have time to look into it right now. > > [snip] > (4) How/why are the following two definitions different? > > module MyApp > def wibble; puts "ok"; end > module_function :wibble > end > MyApp.wibble #==> "ok" > include MyApp > wibble #==> "ok" > ----- > module MyApp > def MyApp.wibble; puts "ok"; end > end > MyApp.wibble #==> "ok" > include MyApp > wibble #==> undefined local variable or method `wibble' for main:Object (NameError) > The first creates two methods, while the second one creates only one message: See: ------------------------------------------------- Module#module_function module_function(symbol, ...) => self ------------------------------------------------------------------------ Creates module functions for the named methods. These functions may be called with the module as a receiver, and also become available >> as instance methods to classes that mix in the module. Module >> functions are copies of the original, and so may be changed independently. The instance-method versions are made private. If used with no arguments, subsequently defined methods become module functions. > Many thanks... I'm no guru, but I hope that I could help a bit. > > Brian. another Brian -- Brian Schr�der http://ruby.brian-schroeder.de/