Use instance_of? for robustness. : instance_of « Reflection « Ruby






Use instance_of? for robustness.


# don't do the block (do |t|...end) unless the variable myString is a string.

myString = "asdf"

if myString.instance_of?( String )
  myString.split.each do |t|
     puts t
  end
end

 








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.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