How to check the data type of an object in Ruby?

Ruby | Checking Type of an Object: Here, we are going to learn how to check the datatype of an object in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on May 05, 2020

We all know that Ruby is a pure object-oriented language and in any pure object-oriented language, every single entity is considered as an object. So, in Ruby also every datatype is considered as a class and every variable is considered as an object. So basically we can say that if we want to find the data type of a variable, we will ultimately have to check the class of that particular object or instance.

If we want to work on objects then we will have to take help from the Object class which is the default class of all objects.

There are two ways through which we can check the class of the objects and they are listed below,

1) With the help of Object.class method

This method is defined in the Object class of Ruby's library and sorely used for checking the class of a particular object or instance. This method returns the class name of the particular instance.

Syntax:

    object_name.class

Program:

=begin
  Ruby program to check the Class of an object
=end

class Student
 def name
 	puts "Hrithik"
 end
end

Obj1 = Student.new
Obj2 = "Includehelp.com"
Obj3 = 12

puts "The class of Obj1 is #{Obj1.class}"
puts "The class of Obj2 is #{Obj2.class}"
puts "The class of Obj3 is #{Obj3.class}"

Output

The class of Obj1 is Student
The class of Obj2 is String
The class of Obj3 is Integer

Explanation:

As you can see in the output, we are finding the class of the object with the help of the Object.class method. You can observe that the first object belongs to the Student class which is a user-defined class. Obj2 and Obj3 are the instances of String and Integer classes respectively which we can find with the help of the Object.class method.

2) With the help of Object.is_a?(Class_name) method

This method is defined in the Object class of Ruby's library and sorely used for checking the class of a particular object or instance. This method returns Boolean value which are true and false. for instance, if the object belongs to the particular class which is passed with the method then the method will return true otherwise false.

Syntax:

    Object_name.is_a?(Class_name)

Program:

=begin
  Ruby program to check the Class of an object
=end

class Student
 def name
 	puts "Hrithik"
 end
end

Obj1 = Student.new
Obj2 = "Includehelp.com"
Obj3 = 12

if (Obj1.is_a?(Student) == true)
	puts "Obj1 is of Student class"
end

if (Obj2.is_a?(String) == true)
	puts "Obj2 is of String class"
end

Output

Obj1 is of Student class
Obj2 is of String class

Explanation:

In the above code, you can observe that we are checking the class of an object with the help of the is_a method. The method is returning true when it finds the object belongs to that particular class.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.