I have three objects that are all the same. One of them was created through Item.new
, and the other two were extracted from the database (Mongoid). Am I passing one or any of these objects to another method and checking the type of this method with is_a?
:
def initialize (item, attrs = nil, options = nil) super(attrs, options) raise 'invalid item object' unless item.is_a?(Item)
OK, this raise gets hit. So I check the class, is_a and instance_of in the rails console. I get conflicting results. Why will they have the same class
, but only one of them will be instance_of
that class
?
>> i0.is_a? Item => false >> i1.is_a? Item => false >> i2.is_a? Item => true >> i0.class => Item >> i1.class => Item >> i2.class => Item >> i0.instance_of?(Item) => false >> i1.instance_of?(Item) => false >> i2.instance_of?(Item) => true
Is there a better way to do type checking on my inputs? Why are three things that are the same class not all instances of this class?
source share