From: Joel VanderWerf Date: 2007-06-29T11:44:30+09:00 Subject: Re: NilClass, TrueClass, FalseClass special cased for singleton classes? John Lam (CLR) wrote: > If you do: > > > > class << nil > > self > > end > > > > it returns NilClass � this is the case for true and false as well. > > > > It appears that this behavior is special-cased for at least these three > well-known singleton objects. Two questions: Is that really a special case, as far as behavior goes? The implementation of these three is special (for example, the use of SPECIAL_SINGLETON in rb_singleton_class() in class.c), but it seems to me that the behavior is the same as: foo = [] FooClass = class << foo; self; end case foo when FooClass; puts "FOO" end Is there any way the programmer can tell the difference? Well, I was able to find three ways, but they are kind of phony, and a program shouldn't depend on these differences, IMO: p NilClass p FooClass puts p NilClass.dup p((FooClass.dup rescue "can't dup FooClass")) puts p Marshal.dump(NilClass) p((Marshal.dump(FooClass) rescue "can't dump FooClass")) __END__ Output: NilClass #> # "can't dup FooClass" "\004\bc\rNilClass" "can't dump FooClass" -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407