From: David Masover Date: 2010-12-02T15:39:50+09:00 Subject: Re: Singleton class, metaclass, eigenclass: what do they mean? On Wednesday, December 01, 2010 11:25:35 pm Tony Arcieri wrote: > Every time I think I have my head around what these terms mean I seem to > run across someone with a completely different definition. > > My understanding was that the singleton class is what you obtain when you > call self.class in instance scope, That doesn't make sense. I'm curious to know where you got that definition, because when I call self.class, I just get the class, nothing "singleton" about it. Maybe that's useful to distinguish it from the metaclass or eigenclass, but I've always just called that the "class" of an object anyway. Ruby's standard library has an implementation of the Singleton pattern, in which there's a class which will only ever have one instance -- though this is Ruby, so you can always cheat -- so I suppose if you had an instance of a singleton class, calling self.class on that would give you a singleton class. But calling self.class on something else, even something that's been completely hacked up with extensions and even with directly modifying its metaclass as below, is still going to give you the same class as you'd get otherwise. That is, when I do this: a = 'foo' b = 'bar' class << b def to_sym :hacked_bar end end module C def has_c? true end end b.extend C Pretty much anything I do to b other than manually overriding the class method, and I still can't tell the difference between a.class and b.class. Both return String, and in every way I've cared to test, it's, well, just String: a.class # String b.class # String a.object_id == b.object_id # true a.object_id == String.object_id # true This is Ruby, so maybe I'm missing something, but it really does just seem to be the class. Is that right? > and that metaclass and eigenclass > are interchangeable terms for what you obtain if you call class << self; > self; end in instance scope. This is correct, as far as I know. Note that you don't need to do this with 'self' necessarily -- you could do: class << some_object; self; end That would give you the metaclass of some_object. I tend to use _why's metaid anyway.