From: Justin Collins Date: 2006-07-26T05:10:17+09:00 Subject: Re: "Self" confusion Justin Collins wrote: > Leslie Viljoen wrote: >> Can someone explain why the following produces different results? In >> another question I wrote that by default, programs run inside the >> definition-space of Object, but now I think that I was wrong: >> >> class Yoghurt >> def self.a >> "yoghurt self" >> end >> def Yoghurt.a >> "yoghurt" >> end >> >> p Yoghurt.a >> p self.a >> end >> >> >> def self.a >> "object self" >> end >> def Object.a >> "object" >> end >> >> p Object.a >> p self.a >> >> ------------------------Output: >> "yoghurt self" >> "yoghurt self" >> "object" >> "object self" >> > Really? Because I get: > > "yoghurt" > "yoghurt" > "object" > "object self" > > :) > > I thought the top-level stuff rain inside an instance of Object? > > Try: > > p Object.class #=> Class, naturally > p self.class #=> Object > > class A > p self.class #=> Class > end > > a = A.new > > class< p self.class #=> Class > end > > So, if we were really in the definition space of Object, then > self.class would be class, not Object. > > > -Justin > I meant to add some "I think"s and "I guess"s in there somewhere. -Justin