鲁比:有点?与instance_of?与 is_a?

发布于 2024-09-26 09:06:59 字数 33 浏览 5 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情徒 2024-10-03 09:06:59

kind_of?is_a? 是同义词。

instance_of? 与其他两个的不同之处在于,如果对象是该确切类的实例,而不是子类,则它仅返回 true

示例:

  • "hello".is_a?对象“hello”.kind_of? Object 返回 true 因为 "hello"String 并且 String的子类>对象
  • 但是 "hello".instance_of?对象返回false

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.

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.
青衫儰鉨ミ守葔 2024-10-03 09:06:59

有什么区别?

从文档中:

- (布尔值) instance_of?(class)
如果obj则返回true 是给定类的实例。

和:

- (布尔值) is_a?(class)
- (布尔值) kind_of?(class )
如果 classobj 的类,或者如果 class<,则返回 true /code> 是 obj 的超类之一或 obj 中包含的模块。


如果不清楚,那么最好知道具体是什么不清楚,以便改进文档。

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

绝不。请改用多态性。

为什么有这么多?

我不会称​​两个为“很多”。他们有两个,因为他们做两件不同的事情。

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.

岁月静好 2024-10-03 09:06:59

使用 respond_to? 询问对象是否响应您需要的方法,这更像 Ruby。这允许最少的接口和实现无意识编程。

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

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.

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.

三生池水覆流年 2024-10-03 09:06:59

我也不会调用两个many(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

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:

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
你是暖光i 2024-10-03 09:06:59

https://stackoverflow.com/a/3893305/10392483 是一个很好的解释......添加更多颜色这个,我倾向于使用 is_a? 表示“基元”(字符串、数组,也许是哈希等),

所以 "hello".is_a?(String), [].is_a?(Array), {}.is_a?(Hash)

对于其他内容,我倾向于使用 instance_of? (Animal.new.instance_of?(Animal)

我说倾向于是因为它不是那么明确。举个例子:

class Animal;end

class Dog < Animal;end

x = Dog.new

x.is_a?(Dog) # => true
x.is_a?(Animal) # => true
x.instance_of?(Dog) # => true
x.instance_of?(Animal) # => false

如你所见,x 既是 Dog 又是 Animal,但它只是狗。

我认为这是一个特殊性问题:

  • 如果我只想知道它是动物而不是植物,我会使用is_a?
  • 如果我关心它是 Dog 而不是 Cat 我会使用 instance_of?

如果我关心的话,你可以进一步考虑。它是 Sighthound 而不是 Bloodhound,假设两者都是 Dog 的子类,那么我可能想让它更具体

。 , is_a?(Animal|Dog|Sighthound) 总是有效,但如果您关心特定的子类,instance_of? 总是更具体。

https://stackoverflow.com/a/3893305/10392483 is a great explanation ... to add some more colour to this, I tend to use is_a? for "primatives" (String, Array, maybe Hash, etc.)

So "hello".is_a?(String), [].is_a?(Array), {}.is_a?(Hash)

For anything else, I tend to use instance_of? (Animal.new.instance_of?(Animal)

I say tend to because it's not quite that clear cut. Take for example:

class Animal;end

class Dog < Animal;end

x = Dog.new

x.is_a?(Dog) # => true
x.is_a?(Animal) # => true
x.instance_of?(Dog) # => true
x.instance_of?(Animal) # => false

As you can see, x is both a Dog and an Animal, but it's only an instance of Dog.

I see it as a question of specificity:

  • If I just want to know that it's an Animal and not a Plant I'll use is_a?
  • If I care that it's a Dog and not a Cat I'll use instance_of?

You can then take this further. If I care that it's a Sighthound and not a Bloodhound, assuming both are subclasses of Dog. Then I may want to make it even more specific.

That said, is_a?(Animal|Dog|Sighthound) will always work. But if you care about the specific subclass, instance_of? is always more specific.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文