确定 ruby​​ 中对象的类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15769739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:51:20  来源:igfitidea点击:

Determining type of an object in ruby

rubytypes

提问by Zippy Zeppoli

I'll use python as an example of what I'm looking for (you can think of it as pseudocode if you don't know Python):

我将使用 python 作为我正在寻找的示例(如果您不了解 Python,您可以将其视为伪代码):

>>> a = 1
>>> type(a)
<type 'int'>

I know in ruby I can do :

我知道在 ruby​​ 中我可以做到:

1.9.3p194 :002 > 1.class
 => Fixnum 

But is this the proper way to determine the type of the object?

但这是确定对象类型的正确方法吗?

回答by tadman

The proper way to determine the "type" of an object, which is a wobbly term in the Ruby world, is to call object.class.

确定对象“类型”的正确方法是调用object.class.

Since classes can inherit from other classes, if you want to determine if an object is "of a particular type" you might call object.is_a?(ClassName)to see if objectis of type ClassNameor derived from it.

由于类可以从其他类继承,如果您想确定一个对象是否是“特定类型的”,您可以调用object.is_a?(ClassName)以查看它是否object属于类型ClassName或从它派生。

Normally type checking is not done in Ruby, but instead objects are assessed based on their ability to respond to particular methods, commonly called "Duck typing". In other words, if it responds to the methods you want, there's no reason to be particular about the type.

通常,Ruby 不会进行类型检查,而是根据对象响应特定方法的能力来评估对象,通常称为“鸭子类型”。换句话说,如果它响应您想要的方法,则没有理由特别关注类型。

For example, object.is_a?(String)is too rigid since another class might implement methods that convert it into a string, or make it behave identically to how String behaves. object.respond_to?(:to_s)would be a better way to test that the object in question does what you want.

例如,object.is_a?(String)太死板了,因为另一个类可能会实现将其转换为字符串的方法,或者使其行为与 String 的行为方式相同。object.respond_to?(:to_s)将是测试有问题的对象是否满足您的要求的更好方法。

回答by Arup Rakshit

you could also try: instance_of?

你也可以尝试: instance_of?

p 1.instance_of? Fixnum    #=> True
p "1".instance_of? String  #=> True
p [1,2].instance_of? Array #=> True

回答by Stuart M

Oftentimes in Ruby, you don't actually care what the object's class is, per se, you just care that it responds to a certain method. This is known as Duck Typingand you'll see it in all sorts of Ruby codebases.

通常在 Ruby 中,您实际上并不关心对象的类是什么,本身,您只关心它响应某个方法。这称为Duck Typing,您会在各种 Ruby 代码库中看到它。

So in many (if not most) cases, its best to use Duck Typing using #respond_to?(method):

因此,在许多(如果不是大多数)情况下,最好使用 Duck Typing 使用#respond_to?(method)

object.respond_to?(:to_i)

回答by Douglas G. Allen

I would say "Yes". As "Matz" had said something like this in one of his talks, "Ruby objects have no types." Not all of it but the part that he is trying to get across to us. Why would anyone have said "Everything is an Object" then? To add he said "Data has Types not objects".

我会说“是”。正如“Matz”在他的一次演讲中所说的那样,“Ruby 对象没有类型。” 不是全部,而是他试图传达给我们的部分。为什么会有人说“一切都是对象”呢?添加他说“数据具有类型而不是对象”。

So we might enjoy this.

所以我们可能会喜欢这个。

https://www.youtube.com/watch?v=1l3U1X3z0CE

https://www.youtube.com/watch?v=1l3U1X3z0CE

But Ruby doesn't care to much about the type of object just the class. We use classes not types. All data then has a class.

但是 Ruby 不太关心对象的类型,而只是类。我们使用类而不是类型。然后所有数据都有一个类。

12345.class

'my string'.class

They may also have ancestors

他们也可能有祖先

Object.ancestors

They also have meta classes but I'll save you the details on that.

他们也有元类,但我会为您保存详细信息。

Once you know the class then you'll be able to lookup what methods you may use for it. That's where the "data type" is needed. If you really want to get into details the look up...

一旦你了解了这个类,你就可以查找你可能使用的方法。这就是需要“数据类型”的地方。如果您真的想了解详细信息,请查看...

"The Ruby Object Model"

“Ruby 对象模型”

This is the term used for how Ruby handles objects. It's all internal so you don't really see much of this but it's nice to know. But that's another topic.

这是用于 Ruby 如何处理对象的术语。这都是内部的,所以你并没有真正看到很多,但很高兴知道。但那是另一个话题。

Yes! The class is the data type. Objects have classes and data has types. So if you know about data bases then you know there are only a finite set of types.

是的!类是数据类型。对象有类,数据有类型。因此,如果您了解数据库,那么您就会知道只有一组有限的类型。

text blocks numbers

文本块数字