From: "Mauricio Fernández" Date: 2003-05-09T16:18:43+09:00 Subject: Re: Can a global be a constant? 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 = 5; end > def self.get; puts @x; end > end They do (well, except for the fact that D::set always sets to 5 :-) There can be one difference, though: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/57274 > 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*. 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 Foo' instance methods: ["foo"] Foo singleton methods:["foo"] Take into account that you'd have to use Someclass.singleton_method_added instead of (class << Someclass; self; end).method_added if you want notification on addition of singleton methods. -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com How do you power off this machine? -- Linus, when upgrading linux.cs.helsinki.fi, and after using the machine for several months