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

The is_a? method in Ruby can be used to check the data type of an object. For example:

str = "Hello"
num = 123

puts str.is_a? String # true
puts num.is_a? Integer # true
puts str.is_a? Integer # false
puts num.is_a? String # false

You can pass the data type you want to check as a parameter to the is_a? method, which will return a boolean value indicating whether the object is of the specified data type.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds