I do the following, and it evaluates to false
:
我这样做,它的值为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?
评估为true。有Ruby背景的人能给我解释一下吗?
1 个解决方案
#1
15
In Ruby, ===
isn't a stricter version of ==
, as it is in some other languages.
在Ruby中,===不是更严格的==,因为它是其他语言的。
The ===
method has several meanings:
==方法有几个含义:
Membership:
(1..10) === 5 # => true
Test whether the argument is an instance of the receiver:
p MyObject.new.class === MyObject.new # true; it's the same as
p MyObject.new.is_a? MyObject
Regex match:
/\w+/ === "Ruby"
Case statements:
year = 2011
case year
when 1901..2000
puts 'Second millennium'
when 2001..2999
puts 'Third millennium'
end
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)
MyObject.new。class == = MyObject只是一个普通的相等性测试(MyObject是一个类对象,myobjecter .new)。类是同一个类对象)
#1
15
In Ruby, ===
isn't a stricter version of ==
, as it is in some other languages.
在Ruby中,===不是更严格的==,因为它是其他语言的。
The ===
method has several meanings:
==方法有几个含义:
Membership:
(1..10) === 5 # => true
Test whether the argument is an instance of the receiver:
p MyObject.new.class === MyObject.new # true; it's the same as
p MyObject.new.is_a? MyObject
Regex match:
/\w+/ === "Ruby"
Case statements:
year = 2011
case year
when 1901..2000
puts 'Second millennium'
when 2001..2999
puts 'Third millennium'
end
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)
MyObject.new。class == = MyObject只是一个普通的相等性测试(MyObject是一个类对象,myobjecter .new)。类是同一个类对象)