Ads by ProfitSence
Close

Type of an object in ruby

Last Updated on Wednesday 5th Oct 2022

ruby check type

ruby object type

The proper way to determine the type of an object, is to call object.class.

Object.class method

			
					object_name.class

			
	
			
					first = "Hello World"
second = 21
puts "The class of first is #{first.class}"
puts "The class of second is #{second.class}"

			
	

ruby type

			
					The class of first is String
The class of second is Integer

			
	

ruby type of

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.

			
					 Object_name.is_a?(Class_name)

			
	

ruby type of object

			
					first = "Hello World"
second = 21

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

if (second.is_a?(Integer) == true)
    puts "Second is of Integer class"
end

			
	
			
					First is of String class
Second is of Integer class