From: Joel VanderWerf Date: 2001-10-31T04:00:58+09:00 Subject: [ruby-talk:23862] Re: sleeping, calling methods inside class defs ts wrote: > > >>>>> "J" == Joel VanderWerf writes: > > J> I'm not so concerned about the public/private difference as about the > J> asymmetry in which class receives the definition. What is is about > J> "main" or "Object" that causes def to attach methods to the "type", in > J> that context, rather than to "self"? Is this just a special case in the > J> interpreter? > > When you define a def it's defined in the current class. > > At toplevel the current class is Object, this mean that you define a > method of Object. > > When you write > > class A > end > > ruby change the current class to A. a def inside A is defined in A Then I guess I don't understand what the "current class" is, or how to determine it with a Ruby expression. You can't just call self.class, as one can see from the following example: class Foo p self, self.class end # ==> # Foo # Class In this context, methods get defined in Foo, not Class. But in the top level: p self, self.class # ==> # main # Object In this context, methods get defined in Object, not main's singleton class. Why is the current class (meaning the place where def puts methods) sometimes obtained by "self" and sometimes by "self.class"? Is there some reliable way of determining where method definitions are going to go? Or do you just have to know whether you're inside a "class ... end" block? I'm confused.... as usual :) -- Joel VanderWerf California PATH, UC Berkeley mailto:vjoel@path.berkeley.edu Ph. (510) 231-9446 http://www.path.berkeley.edu FAX (510) 231-9512