Ruby:kind_of ?与instance_of吗?与is_a吗?

时间:2021-05-13 22:28:52

What is the difference? When should I use which? Why are there so many of them?

的区别是什么?我应该什么时候使用哪个?为什么有这么多呢?

4 个解决方案

#1


536  

kind_of? and is_a? are synonymous. instance_of? is different from the other two in that it only returns true if the object is an instance of that exact class, not a subclass.

kind_of吗?和is_a吗?是同义的。instance_of吗?与其他两个不同的是,它只在对象是那个类的实例而不是子类的实例时返回true。

Example: "hello".is_a? Object and "hello".kind_of? Object return true because "hello" is a String and String is a subclass of Object. However "hello".instance_of? Object returns false.

例如:“你好”.is_a吗?对象和“你好”.kind_of吗?对象返回true,因为“hello”是一个字符串,字符串是对象的子类。然而“hello”.instance_of吗?对象返回false。

#2


14  

What is the difference?

的区别是什么?

From the documentation:

从文档:

- ( Boolean) instance_of?(class)
Returns true if obj is an instance of the given class.

and:

和:

- ( Boolean) is_a?(class)
- ( Boolean) kind_of?(class)
Returns true if class is the class of obj, or if class is one of the superclasses of obj or modules included in obj.

If that is unclear, it would be nice to know what exactly is unclear, so that the documentation can be improved.

如果这是不清楚的,那么最好知道什么是不清楚的,这样文档就可以得到改进。

When should I use which?

我应该什么时候使用哪个?

Never. Use polymorphism instead.

从来没有。使用多态。

Why are there so many of them?

为什么有这么多呢?

I wouldn't call two "many". There are two of them, because they do two different things.

我不会叫两个“很多”。有两个,因为它们做了两件不同的事。

#3


5  

It is more Ruby-like to ask objects whether they respond to a method you need or not, using respond_to?. This allows both minimal interface and implementation unaware programming.

使用respond_to?询问对象是否对您需要的方法做出响应更像ruby。这允许最小化的接口和实现不知道编程。

It is not always applicable of course, thus there is still a possibility to ask about more conservative understanding of "type", which is class or a base class, using the methods you're asking about.

当然,它并不总是适用的,因此仍然有可能询问对“type”(类或基类)的更保守理解,使用您所询问的方法。

#4


3  

I also wouldn't call two many (is_a? and kind_of? are aliases of the same method), but if you want to see more possibilities, turn your attention to #class method:

我也不会叫两个多(is_a?和kind_of吗?是相同方法的别名),但是如果您想看到更多的可能性,请将注意力转向#class方法:

A = Class.new
B = Class.new A

a, b = A.new, B.new
b.class < A # true - means that b.class is a subclass of A
a.class < B # false - means that a.class is not a subclass of A
# Another possibility: Use #ancestors
b.class.ancestors.include? A # true - means that b.class has A among its ancestors
a.class.ancestors.include? B # false - means that B is not an ancestor of a.class

#1


536  

kind_of? and is_a? are synonymous. instance_of? is different from the other two in that it only returns true if the object is an instance of that exact class, not a subclass.

kind_of吗?和is_a吗?是同义的。instance_of吗?与其他两个不同的是,它只在对象是那个类的实例而不是子类的实例时返回true。

Example: "hello".is_a? Object and "hello".kind_of? Object return true because "hello" is a String and String is a subclass of Object. However "hello".instance_of? Object returns false.

例如:“你好”.is_a吗?对象和“你好”.kind_of吗?对象返回true,因为“hello”是一个字符串,字符串是对象的子类。然而“hello”.instance_of吗?对象返回false。

#2


14  

What is the difference?

的区别是什么?

From the documentation:

从文档:

- ( Boolean) instance_of?(class)
Returns true if obj is an instance of the given class.

and:

和:

- ( Boolean) is_a?(class)
- ( Boolean) kind_of?(class)
Returns true if class is the class of obj, or if class is one of the superclasses of obj or modules included in obj.

If that is unclear, it would be nice to know what exactly is unclear, so that the documentation can be improved.

如果这是不清楚的,那么最好知道什么是不清楚的,这样文档就可以得到改进。

When should I use which?

我应该什么时候使用哪个?

Never. Use polymorphism instead.

从来没有。使用多态。

Why are there so many of them?

为什么有这么多呢?

I wouldn't call two "many". There are two of them, because they do two different things.

我不会叫两个“很多”。有两个,因为它们做了两件不同的事。

#3


5  

It is more Ruby-like to ask objects whether they respond to a method you need or not, using respond_to?. This allows both minimal interface and implementation unaware programming.

使用respond_to?询问对象是否对您需要的方法做出响应更像ruby。这允许最小化的接口和实现不知道编程。

It is not always applicable of course, thus there is still a possibility to ask about more conservative understanding of "type", which is class or a base class, using the methods you're asking about.

当然,它并不总是适用的,因此仍然有可能询问对“type”(类或基类)的更保守理解,使用您所询问的方法。

#4


3  

I also wouldn't call two many (is_a? and kind_of? are aliases of the same method), but if you want to see more possibilities, turn your attention to #class method:

我也不会叫两个多(is_a?和kind_of吗?是相同方法的别名),但是如果您想看到更多的可能性,请将注意力转向#class方法:

A = Class.new
B = Class.new A

a, b = A.new, B.new
b.class < A # true - means that b.class is a subclass of A
a.class < B # false - means that a.class is not a subclass of A
# Another possibility: Use #ancestors
b.class.ancestors.include? A # true - means that b.class has A among its ancestors
a.class.ancestors.include? B # false - means that B is not an ancestor of a.class