它不是指针或object_id.红宝石允许你排序的治疗数组作为值,所以包含相同的元素两个阵列产生相同的hash值.
在这里,看看:
arr1 = [1, 2]
arr2 = [1, 2]
# You'll see false here
puts arr1.object_id == arr2.object_id
# You'll see true here
puts arr1.hash == arr2.hash
hash = {}
hash[arr1] = 'foo'
hash[arr2] = 'bar'
# This will output {[1, 2] => 'bar'},
# so there's only one entry in the hash
puts hash
Run Code Online (Sandbox Code Playgroud)
HashRuby中的类使用hash对象的方法来确定其作为键的唯一性.这就是为什么arr1并且arr2在上面的代码中可以互换(作为键).