How to use Class method In Ruby
The Class
method returns the class name of an object.
Syntax:
variable.class
Example:
In this example, we use the class method on num,string and an array
to determine its data type, which returns Integer
.
# Check the Data Type of a Variable using 'class' method
# Example 1: Integer
num = 10
puts "Data type of 'num': #{num.class}"
# Example 2: String
str = "w3wiki"
puts "Data type of 'str': #{str.class}"
# Example 3: Array
arr = [1, 2, 3]
puts "Data type of 'arr': #{arr.class}"
Output
Data type of 'num': Integer Data type of 'str': String Data type of 'arr': Array
How to Check the Data Type of a Variable in Ruby?
In this article, we will discuss how to check the data type of a variable in Ruby. We can check the data types of a variable through different methods ranging from class to kind_of method in Ruby.
Table of Content
- Using Class method
- Using is_a? Method
- Using kind_of? Method
Contact Us