Ruby Hash的rehash函数
Hash#rehash() : rehash()是一个Hash类方法,它基于当前的哈希值为每个键重建哈希。如果键对象的值在插入后发生了变化,这将重新索引哈希。
语法。Hash.rehash()
参数:哈希值
返回:基于当前的哈希值,为每个键重建哈希值。
例子 #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}