📜  红宝石 |哈希再哈希函数

📅  最后修改于: 2022-05-13 01:54:18.353000             🧑  作者: Mango

红宝石 |哈希再哈希函数

Hash#rehash() : rehash()是一个 Hash 类方法,它基于当前的哈希值重建每个键的哈希值。如果键对象的值自插入后发生更改,这将重新索引哈希。

示例 #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}