From: "David A. Black" Date: 2005-05-16T01:11:07+09:00 Subject: Re: infinite number of singleton_classes Hi -- On Sun, 15 May 2005, Lionel Thiry wrote: > Hello! > > ----8<---- > a = Object.new > > b = class << a > class << self > class << self > class << self > # "class << self".times(n) > self > # "end".times(n) > end > end > end > end > # => #>>>> > ----8<---- > > How many levels can we reach? It seems there is no limit. > > I suppose those singleton_class exists only when needed? Otherwise, I really > wonder where does ruby hide all those singleton_classes... > > But, sincerely, is it needed to have the singleton_class of a singleton_class? > Isn't it a non-sense? Shouldn't this raise an error? [...] > What do you think of it? Isn't it worth an RCR? No; it's just a consequence of the underlying design principles. Yes, there are a few objects that can't have a singleton class (Fixnums and Symbols are the ones I can think of). But Class objects can, and singleton classes are Class objects. So you'd be asking for special-case treatment for a subset of classes. It would be a bit like saying: Surely no one will ever use a singleton class as a hash key, so let's remove the #hash method from it. There's just no reason to constrain the language and its usage that way. Also, I don't see any harm or danger here. I suppose one could accidentally write a loop that kept creating singleton classes, but one could also write a loop that kept creating *non*-singleton classes -- or arrays, or hashes, or MyClass objects, and so on. David -- David A. Black dblack@wobblini.net