views:

52

answers:

3
Apple < ActiveRecord:Base
Orange < ActiveRecord:Base

piece_of_fruit = Apple.new

I want to know whether piece_of_fruit is an Apple or an Orange - even though both are derived from ActiveRecord:Base.

Is there a reflection method that will tell me the next class in the inheritance tree (Apple/Orange).

What about if I want to look at each successive step in the inheritance hierarchy after that, starting with ActiveRecord:Base in this case?

+2  A: 

How about

piece_of_fruit.kind_of?(Apple)
Kathy Van Stone
A: 

is_a and kind_of are synonymous, so you can write either of the following:

piece_of_fruit.is_a?(Apple)

or

piece_of_fruit.kind_of?(Apple)
ski
+2  A: 

The answer to your first question, as others have posted, is

piece_of_fruit.is_a? Apple

The answer to your second question ("What if I want to look at each successive step in the inheritance hierarchy?") is to use the class and superclass methods.

piece_of_fruit.class 
=> Apple
piece_of_fruit.class.superclass     
=> ActiveRecord::Base
Bret Pettichord