红宝石 |哈希再哈希函数
Hash#rehash() : rehash()是一个 Hash 类方法,它基于当前的哈希值重建每个键的哈希值。如果键对象的值自插入后发生更改,这将重新索引哈希。
Syntax: Hash.rehash()
Parameter: Hash values
Return: based on the current hash value rebuilds the hash for each key
示例 #1:
# Ruby code for Hash.rehash() method
# declaring Hash value
a = {a:100, b:200}
# declaring Hash value
b = {a:100, c:300, b:200}
# declaring Hash value
c = {a:100}
# rehash Value
puts "Hash a rehash form : #{a.rehash("a")}\n\n"
puts "Hash b rehash form : #{b.rehash("c")}\n\n"
puts "Hash c rehash form : #{c.rehash("a")}\n\n"
输出 :
Hash a rehash form : false
Hash b rehash form : false
Hash c rehash form : false
示例 #2:
# Ruby code for Hash.rehash() method
# declaring Hash value
a = { "a" => 100, "b" => 200 }
# declaring Hash value
b = {"a" => 100}
# declaring Hash value
c = {"a" => 100, "c" => 300, "b" => 200}
# rehash Value
puts "Hash a rehash form : #{a.rehash()}\n\n"
puts "Hash b rehash form : #{b.rehash()}\n\n"
puts "Hash c rehash form : #{c.rehash()}\n\n"
输出 :
Hash a rehash form : {"a"=>100, "b"=>200}
Hash b rehash form : {"a"=>100}
Hash c rehash form : {"a"=>100, "c"=>300, "b"=>200}