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?
@template.is_a?(MessengerTemplate)?True. Isn'tJobTemplatetechnically an instance ofMessengerTemplate? I know if I did this inJavatheinstanceOfmethod would have passed as indicated here: stackoverflow.com/questions/6304056/…JobTemplateis a subclass ofMessengerTemplate. @template is an instance ofJobTemplate. Please see answer for details. I don't know nothing about no Java.puts @template.instance_of? MessengerTemplateso that it returnstrueif@templateis an instance ofMessengerTemplateor a subclass ofMessengerTemplate. If so, please correct the statement of the problem.