📜  红宝石 |哈希拒绝!方法

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

红宝石 |哈希拒绝!方法

Hash#reject!()是一个 Hash 类方法,用于检查是否在哈希数组中进行了任何更改

示例 #1:

# Ruby code for Hash.reject!() 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}
  
  
# reject! Value
puts "Hash a reject! form : #{a.reject! {|key, value| key < "b"}}\n\n"

输出 :

Hash a reject! form : {"b"=>200}

示例 #2:

# Ruby code for Hash.reject!() method
  
# declaring Hash value
b = {"a" => 100}
  
# declaring Hash value
c = {"a" => 100, "c" => 300, "b" => 200}
  
  
# reject! Value
  
puts "Hash b reject! form : #{b.reject!{|key, value| value < 200}}\n\n"
  
puts "Hash c reject! form : #{c.reject!{|key, value| key < "b"}}\n\n"

输出 :

Hash b reject! form : {}

Hash c reject! form : {"c"=>300, "b"=>200}