Idiom or method for determining where in the class hierarchy anobject fits

C

Collins

I have a class hierachy that looks like this

class A
....
end

class B < A
....
end

class C < A
....
end

class D < B
....
end

class E < C
....
end

class F < B
....
end

Now class B has a method that expects either members of class D or E
or Fas a parameter but needs to do something different based upon the
whether the parameter is a subclass of B or a subclass of C.
I could check to see if it was D or F as opposed to B but that would
mean when I added class G<B...end I'd have to go back and touch B.
Is there a way to ask an object if it is descended from a particular
class? I suppose I could add a method to B and test for the presence
of that method in the parameter objects but that feels ucky.

Thanks

Collins
 
M

Mike Stok

I have a class hierachy that looks like this

class A
...
end

class B < A
...
end

class C < A
...
end

class D < B
...
end

class E < C
...
end

class F < B
...
end

Now class B has a method that expects either members of class D or E
or Fas a parameter but needs to do something different based upon the
whether the parameter is a subclass of B or a subclass of C.
I could check to see if it was D or F as opposed to B but that would
mean when I added class G<B...end I'd have to go back and touch B.
Is there a way to ask an object if it is descended from a particular
class? I suppose I could add a method to B and test for the presence
of that method in the parameter objects but that feels ucky.

Thanks

Collins

You could try kind_of? e.g.
=> false

etc.

Hope this helps,

Mike

--

Mike Stok <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
 
C

Collins

You could try kind_of? e.g.

 >> o = F.new
 => #<F:0x00000100a74a30>
 >> o.kind_of?(A)
 => true
 >> o.kind_of?(B)
 => true
 >> o.kind_of?(C)
 => false

etc.

Hope this helps,

Mike

--

Mike Stok <[email protected]>http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

Thank you! I was only aware of instance_of? This is exactly what I
was hoping to find...

Thanks again

Collins
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top