Is it a number : instance_of « Reflection « Ruby






Is it a number


x = 1                    # This is the value we're working with
x.instance_of? Fixnum    # true: is an instance of Fixnum
x.instance_of? Numeric   # false: instance_of? doesn't check inheritance

 








Related examples in the same category

1.if an object is an instance of a given class with the instance_of? method from Object
2.Use instance_of? for robustness.
3.Unlike instance_of?, is_of? or kind_of? also work if the argument is a superclass or module.
4.Is it a string class instance
5.Use === to check the class type