ruby 数据类型_如何在Ruby中检查对象的数据类型?

本文介绍如何在Ruby中检查对象的数据类型,通过Object.class方法和Object.is_a?方法,了解Ruby面向对象特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ruby 数据类型

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.

我们都知道Ruby是一种纯面向对象的语言,在任何纯面向对象的语言中,每个单独的实体都被视为一个对象。 因此,在Ruby中,每个数据类型也被视为一个类,而每个变量都被视为一个对象。 因此,基本上可以说,如果我们要查找变量的数据类型,则最终将不得不检查该特定对象或实例的类。

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.

如果要处理对象,则必须从Object类(这是所有对象的默认类)中获取帮助。

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

我们可以通过两种方法检查对象的类,下面列出了它们:

1)借助Object.class方法 (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.

该方法在Ruby的库的Object类中定义,非常用于检查特定对象或实例的类。 此方法返回特定实例的类名称。

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.

如您在输出中看到的,我们正在使用Object.class方法来查找对象的类。 您可以观察到第一个对象属于Student类,后者是用户定义的类。 Obj2Obj3分别是String和Integer类的实例,我们可以在Object.class方法的帮助下找到它们。

2)借助Object.is_a?(Class_name)方法 (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.

该方法在Ruby的库的Object类中定义,非常用于检查特定对象或实例的类。 此方法返回布尔值truefalse 。 例如,如果对象属于方法所传递的特定类,则该方法将返回true,否则返回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.

在上面的代码中,您可以观察到我们正在使用is_a方法检查对象的类。 当发现对象属于该特定类时,该方法将返回true。

翻译自: https://www.includehelp.com/ruby/how-to-check-the-datatype-of-an-object.aspx

ruby 数据类型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值