if an object is an instance of a given class with the instance_of? method from Object : instance_of « Reflection « Ruby






if an object is an instance of a given class with the instance_of? method from Object


myString = "asdf"

puts myString.instance_of? String # => true
puts myString.instance_of? Fixnum # => false

 








Related examples in the same category

1.Use instance_of? for robustness.
2.Unlike instance_of?, is_of? or kind_of? also work if the argument is a superclass or module.
3.Is it a string class instance
4.Is it a number
5.Use === to check the class type