From: Dave Thomas Date: 2000-07-06T06:53:50-05:00 Subject: [ruby-talk:03867] Re: main.method Aleksi Niemel� writes: > How I can make this work, that is to print blop and blop instead of blop and > burp? How to call methods defined in Object just like methods in any other > class? That means the method don't get wrong self? > > I thought main.burp would deal with the problem, but it seems that I can't > access main. 'main' is just the global object's representation under to_s. It isn't a variable or reserved word. A second problem is that methods defined at the top-level are private, so they cannot be called with a receiver, so you can't go messing with their owning object without doing some messing, such as: class Fish def initialize @burp = "burp" end def burp(global) puts global.globalBurp end end @burp = "blop" def globalBurp p self @burp end public :globalBurp puts globalBurp, "\n" puts "now I expect fish to (blobal) burp, which means 'blop'" Fish.new.burp(self) ###################### Outputs: # blop now I expect fish to (blobal) burp, which means 'blop' # blop Another way would be to use the special TOPLEVEL_BINDING to get to those instance variables: class Test def printTop puts eval("@top", TOPLEVEL_BINDING) end end @top = "this is top" Test.new.printTop However, I'm forced to ask -- why would you ever want to do this? Regards Dave