From: Tim Pease Date: 2009-04-07T08:08:49+09:00 Subject: Re: class C < B < A On Apr 6, 2009, at 4:12 PM, 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: > > a) > a *** A => true > a *** B => false > a *** C => false > > b) > b *** A => false (*1) > b *** B => true > b *** C => false > > c) > c *** A => false (*2) > c *** B => true > c *** C => true > > > Note that I could use ===, so: > A === a => true > A === b => false > > but in cases *1 and *2 the result is not what I want: > > A === b => true (I want false) > A === c => true (I want false) > Well, it's kinda hackish but it does give the correct output. Tailor to suit your needs: class A alias :like? :instance_of? end class B < A; end class C < B def like?( clazz ) return true if clazz == B super end end a = A.new b = B.new c = C.new [A, B, C].each do |clazz| puts "a.like? #{clazz.name} => #{a.like? clazz}" end puts [A, B, C].each do |clazz| puts "b.like? #{clazz.name} => #{b.like? clazz}" end puts [A, B, C].each do |clazz| puts "c.like? #{clazz.name} => #{c.like? clazz}" end ==== OUTPUT ==== a.like? A => true a.like? B => false a.like? C => false b.like? A => false b.like? B => true b.like? C => false c.like? A => false c.like? B => true c.like? C => true Again, I'm not proud of this code -- it works, though, without being too tricky. Blessings, TwP