Ruby Hash reject! 方法
Hash#reject!() 是一个Hash类的方法,它可以检查哈希数组中是否有任何变化。
语法。Hash.reject!()
参数:哈希值
返回:true – 如果有变化,否则返回false
例子 #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}