From: Robert Klemme Date: 2009-04-07T16:49:11+09:00 Subject: Re: class C < B < A On 07.04.2009 00:41, Iñaki Baz Castillo wrote: > El Martes 07 Abril 2009, Joel VanderWerf escribió: >> Iñaki Baz Castillo wrote: >>> Hi, I have this case: >>> >>> class A ; end >>> class B < A ; end >>> class C < B ; end >>> >>> a = A.new >>> b = B.new >>> c = C.new >>> >>> >>> I look for a way (***) to get the following results: >> ... >> >> #instance_of? works for all cases except: >>> c *** B => true >> Are you sure you need this to be true? > > Yes. Basically A is String and B is a child of String with various methods, > and C is a more specific class. Frankly, I find this a bit spooky for several reasons: first, you inherit String which IMHO is almost always a bad idea(tm). You should consider using delegation, so you have class B attr_reader :str end class C < B; end Then, you introduce inconsistencies in inheritance checking. That might yield all sorts of surprises. If you want to do it nevertheless, I'd probably go for a special method which you override appropriately: class Module def class_of?(obj) if self == String self == obj.class else self === obj end end end class B < String end class C < B end classes = [String,B,C] classes.each do |cl| puts cl o = cl.new classes.each do |cl2| print cl2, " ", cl2.class_of?(o), "\n" end puts "--" end Something along these lines. Kind regards robert