为什么 MyObject.new.class === MyObject 的计算结果为 false?
我执行以下操作,其计算结果为 false
:
MyObject.new.class === MyObject
但是,
MyObject.new.class == MyObject
计算结果为 true
。有更多 Ruby 背景的人可以向我解释一下这一点吗?是否可以使用 ==
来实现此目的?
I do the following, and it evaluates to false
:
MyObject.new.class === MyObject
However,
MyObject.new.class == MyObject
evaluates to true
. Can someone with a bit more Ruby background explain this to me, and if it's okay to use ==
for this purpose?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Ruby 中,
===
并不是==
的更严格版本,这与其他一些语言不同。===
方法有几个含义:成员资格:
测试参数是否是接收者的实例:
正则表达式匹配:
Case 语句:
其他含义,请参见
ri ===
MyObject.new.class == MyObject
只是普通的相等测试(MyObject 是一个类对象,MyObject.new.class 是同一个类对象)In Ruby,
===
isn't a stricter version of==
, as it is in some other languages.The
===
method has several meanings:Membership:
Test whether the argument is an instance of the receiver:
Regex match:
Case statements:
Other meanings, see
ri ===
MyObject.new.class == MyObject
is just a normal equality test (MyObject is a class object, and MyObject.new.class is the same class object)