Reputation: 3164
I've got the following three classes:
class MessageBuilder
def initialize(template)
@template = template
puts @template.instance_of? MessengerTemplate
end
end
class MessengerTemplate
def initialize
@default_template_id = "111111"
end
end
class JobTemplate < MessengerTemplate
def initialize(name)
@name = name
@template_id = "2222"
end
end
I'm trying to check if a parameter passed to MessageBuilder#initialize
is an instance of MessengerTemplate
. If not, I need to throw an error.
When I call:
message = MessageBuilder.new(JobTemplate.new("Invoice"))
the following line in the constructor:
puts @template.instance_of? MessengerTemplate
prints FALSE
.
Can someone please tell me what I am doing wrong here?
Upvotes: 0
Views: 1706
Reputation: 20263
Try:
@template.is_a?(MessengerTemplate)
As noted in the docs:
Returns true if class is the class of obj, or if class is one of the superclasses of obj or modules included in obj.
MessengerTemplate
is a superclass of @template
, therefore @template.is_a?(MessengerTemplate) => true
.
Upvotes: 6