How to check the type of a variable is Ruby| Ruby on Rails By Example

Sometimes, We want to check an object type programmatically to categorize or store it into specific variables. Java provides instanceOf, and Javascript provides typeOf methods to detect instance type.

Ruby provides different ways and methods for instance or object or variable type at runtime.

Sometimes, We want to check a variable type. The type can be an int or any object.

Variable can be number, float, boolean, array, or hash.

There are multiple ways we can check the type of a variable.

  • class attribute
  • instance_of? method
  • is_a? method
  • kind_of method
  • includes boolean

How to check variable type in Ruby

Ruby provides a class property, that returns the class of a variable.

Each variable has this property It is always called with variable.

Here is a syntax:

variable_name.class

Here is a complete example

employees = {'frank' => 5000, 'Andrew' => 6000, 'Rocky' => 10000}
array=[1,2,3,4,5];
puts 1.class
puts 1.0.class
puts  'str'.class
puts false.class
puts true.class
puts employees.class
puts array.class
puts nil.class

Output:

Integer
Float
String
FalseClass
TrueClass
Hash
Array
NilClass

In the above example, the class returns the following things.

  • normal number returns Integer Class
  • floating numbers return Float Class
  • a collection of characters enclosed in quotes returns String Class
  • Boolean values returns FalseClass or TrueClass Class
  • Objects return Hash Class
  • Array variable returns Array Class
  • Nil value returns NilClass

How to check given variable is an array or Hash?

variable class property returns the class.

Use this class in an if conditional statement to return true or false. We can check given variable is an array or not in many ways. There are multiple ways we can check with array properties and methods.

To check if a variable is an array in ruby.

array=[1,2,3,4,5];
puts array.class
if (array.class == Array)
 puts "Variable is an array\n"
else
 puts "Variable is not an array\n"
end

Output:

Variable is an array

To check if the given variable is a Hash or not.

employees = {'frank' => 5000, 'Andrew' => 6000, 'Rocky' => 10000}
if (employees.class == Hash)
 puts "Variable is an Hash\n"
else
 puts "Variable is not an Hash\n"
end

Output:

Variable is a Hash

There are other ways, we can check using kind_of and is_a functions

value1 = 1;
value2 = 2
puts value1.kind_of? Integer
puts value1.is_a? Integer

How to check if a Ruby object is a Boolean?

We can also check boolean variables using an array of boolean values with the include method.

Here is an example

isCheck = true;
isCheck1 = false
 puts [true,false].include?(isCheck)
 puts [true,false].include?(isCheck1)

How to check variable object type in Ruby Language

This is another way to test object or instance type in Ruby Language

instance_of method checks instance with a class and returns true if a variable is an of type class.

Here is an example

employees = {'frank' => 5000, 'Andrew' => 6000, 'Rocky' => 10000}
 puts employees.instance_of? Hash
puts 12.instance_of? Fixnum
puts "Strtest".instance_of? String
puts [11,21].instance_of? Array

Output:

true
true
true
true

Conclusion

Ruby provides a property class on a defined variable which returns the datatype of a variable.

Also, You can check if a variable is a hash or array.