Ruby class Method Tutorial

In this section, we will learn what the class method is and how to use it in Ruby.

What is class Method in Ruby?

The Ruby `class` method can be used on top of objects to see the class that they are built from.

For example, if we call this method on an integer value, we will get the value “Integer”. If we call this method on top of the `true` value, we will get `TrueClass` etc.

Example: using class method in Ruby

puts "string value".class

puts 10.class

puts true.class

puts [1,2,3,4].class

Output

String

Integer

TrueClass

Array

Example: class method in Ruby

if "string value".class == String

    puts "The String value is of type String class"

end

Output:

The String value is of type String class
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies