Devin Mullins
1/2/2006 7:48:00 PM
Antti Karanta wrote:
> So what is the logic behind the "main" object? Is there a logical reason
>for the methods defined in its context to appear as private methods of
>class Object?
>
>
To quote Guy Decoux:
> no, not really. ruby has self and *internally* ruby_class which give it
> where the method must be defined.
>
> For example :
>
> * at top level it has : self = main, ruby_class = Object
>
> when you define a method at top level this will be an Object method
>
> * in the class A, it has : self = A, ruby_class = A
>
> when you define a method in A, this will be an instance method for A
>
Now, I have a question for Guy:
Flagellate = Class.new {
def banana_boat; puts 'hurrah!' end
}
makes banana_boat a method on Flagellate, which makes me think that
methods go where self is.
However:
class FranklinRoosevelt
def smooth_operator; def banana_boat; puts 'javohl!' end end
end
FranklinRoosevelt.new.smooth_operator
makes banana_boat a method on FranklinRoosevelt, which is either
ruby_class or self.class, but not self.
What's goin' on? Is there another internal variable that determines
where methods go, or am I confusing things? Are Class.new and class_eval
just the exception to the rule that method definitions go on ruby_class?
If so, how?
Devin