From: Jim Freeze Date: 2003-05-09T21:14:58+09:00 Subject: Re: Can a global be a constant? On Friday, 9 May 2003 at 16:18:43 +0900, Mauricio Fern�ndez wrote: > On Fri, May 09, 2003 at 01:13:51PM +0900, Jim Freeze wrote: > > So, fundamentally, is the code in both class C and D below > > doing the same thing? > > > > class C > > class << self > > def set(x); @x = x; end > > def get; puts @x; end > > end > > end > > > > class D > > def self.set; @x = x; end > > def self.get; puts @x; end > > end > > > If these class methods have instance variables for the > > singleton object, are class methods not just really > > instance methods of the singleton object? > > I don't think "singleton object" is standard terminology here. > Regardless of the use of class instance variables, class methods are in > fact singleton methods of the object that represents the class (i.e., > that of type Class). You can also view them as instance methods of the > associated *singleton class*. > Hmm, associated singleton class seems weird to me. Why does an object (in this case a class object) have an associated singleton class? Don't objects just have single methods added to them? > batsman@tux-chan:/tmp$ expand -t2 ad.rb > > class Foo > puts "Foo.id: #{id}" > class << self > def foo > end > > puts "Foo'.id: #{id}" > puts "Foo' instance methods: " + instance_methods.inspect > end > puts "Foo singleton methods:" + singleton_methods.inspect > end > > batsman@tux-chan:/tmp$ ruby ad.rb > Foo.id: 537725700 > Foo'.id: 537725690 I'm having troubles understanding the Foo and Foo' objects. Can you explain more? -- Jim Freeze ---------- Misery no longer loves company. Nowadays it insists on it. -- Russell Baker