鲁比:有点?与instance_of?与 is_a?
有什么区别?我什么时候应该使用哪个?为什么有这么多?
What is the difference? When should I use which? Why are there so many of them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有什么区别?我什么时候应该使用哪个?为什么有这么多?
What is the difference? When should I use which? Why are there so many of them?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
kind_of?
和is_a?
是同义词。instance_of?
与其他两个的不同之处在于,如果对象是该确切类的实例,而不是子类,则它仅返回true
。示例:
"hello".is_a?对象
和“hello”.kind_of? Object
返回true
因为"hello"
是String
并且String
是的子类>对象
。"hello".instance_of?对象
返回false
。kind_of?
andis_a?
are synonymous.instance_of?
is different from the other two in that it only returnstrue
if the object is an instance of that exact class, not a subclass.Example:
"hello".is_a? Object
and"hello".kind_of? Object
returntrue
because"hello"
is aString
andString
is a subclass ofObject
."hello".instance_of? Object
returnsfalse
.从文档中:
和:
如果不清楚,那么最好知道具体是什么不清楚,以便改进文档。
绝不。请改用多态性。
我不会称两个为“很多”。他们有两个,因为他们做两件不同的事情。
From the documentation:
and:
If that is unclear, it would be nice to know what exactly is unclear, so that the documentation can be improved.
Never. Use polymorphism instead.
I wouldn't call two "many". There are two of them, because they do two different things.
使用
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.
我也不会调用两个many(
is_a?
和kind_of?
是同一方法的别名),但如果您想看到更多可能性,请将注意力转向<代码>#class方法:I also wouldn't call two many (
is_a?
andkind_of?
are aliases of the same method), but if you want to see more possibilities, turn your attention to#class
method: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)
我说倾向于是因为它不是那么明确。举个例子:
如你所见,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:
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:
Animal
and not aPlant
I'll useis_a?
Dog
and not aCat
I'll useinstance_of?
You can then take this further. If I care that it's a
Sighthound
and not aBloodhound
, assuming both are subclasses ofDog
. 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.