我有一个哈希,让我们说:
ahash = {test1: "test1",test2: "test2"}
为什么哈希=== ahash返回true,但是ahash ===哈希没有?这是一些使用===和类名的默认ruby行为吗?
这就是===
方法的工作原理.它是定向的,适用于任何类:
"foo" === String
# => false
String === "foo"
# => true
这是因为它评估为:
"foo".send(:===,String)
String.send(:===,"foo")
这些是两种不同的方法,一种是类,一种用于实例.
如果你只关心课堂信息:
"foo".is_a?(String)
# => true
{ }.is_a?(Hash)
# => true
这种方法使您更清楚您的意图.