From: Robert Klemme Date: 2005-05-09T06:39:34+09:00 Subject: Re: Query about the top level object "Gavri Fernandez" schrieb im Newsbeitrag news:e3ecfac70505081403528b2725@mail.gmail.com... > Hi everyone, > > From the pickaxe first edition: > "At the top level, we're executing code in the context of some > predefined object. When we define methods, we're actually creating > (private) singleton methods for this object." > > Is this true? It is true that methods defined at the top-level are > defined as singleton methods of "main" - the top level object? I > thought that the methods added at the top-level are defined as private > methods of Object. (Where I learnt this from, I'm not sure). > Experimenting gave me the latter result. > >>def whoa >> puts "in whoa" >>end >>puts Object.new.private_methods.find {|x| x == "whoa"} > > prints "whoa\n" to the screen. > > So is the pickaxe wrong? Or am I missing something big? This is interesting: $ ruby -e 'def xxxx() end; [Object.instance_methods, private_methods, Object.new.methods, Object.new.private_methods].each {|m| p(m.grep(/xxxx/))}' [] ["xxxx"] [] ["xxxx"] $ ruby -v ruby 1.8.2 (2004-12-25) [i386-cygwin] I guess it has something to do with the implementation of #private_methods... Kind regards robert