From: Joel VanderWerf Date: 2001-10-30T03:33:20+09:00 Subject: [ruby-talk:23740] Re: sleeping, calling methods inside class defs ts wrote: > In reality "global function" don't exist in ruby, and tt is just a private > method of Object. > > Your first 2 scripts work because > * at toplevel, class is Object and self is an instance of Object > * Object is the root of the hierarchy (i.e. A inherit from Object) This has always been a bit confusing to me, though it is useful. In the top level, we have p self, type # ==> # main # Object When you use "def" here, you get private instance methods of Object. Inside a class definition, however: class Foo p self, type end # ==> # Foo # Class OK so far, but when you use "def" in this context, you get public instance methods of Foo. I'm not so concerned about the public/private difference as about the asymmetry in which class receives the definition. What is is about "main" or "Object" that causes def to attach methods to the "type", in that context, rather than to "self"? Is this just a special case in the interpreter? Maybe the top level effectively enclosed in "main.instance_eval {...}"? self.instance_eval { p self, type } # ==> # main # Object So this context agrees with the top level, at least as far as self and type. But that can't explain the asymmetry, because inside this instance_eval context, def produces singleton methods on main, not private instance methods of Object. What does explain this difference? -- Joel VanderWerf California PATH, UC Berkeley mailto:vjoel@path.berkeley.edu Ph. (510) 231-9446 http://www.path.berkeley.edu FAX (510) 231-9512