📜  红宝石 |哈希拒绝方法

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

红宝石 |哈希拒绝方法

Hash#reject()是一个 Hash 类方法,它返回一个新的散列,该散列由块返回 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"
  
puts "Hash b reject form : #{b.reject{|key, value| value < 200}}\n\n"

输出 :

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

Hash b reject form : {}

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