Hash#select():select()是一種Hash類方法,它根據塊條件從哈希中查找數組。
用法:Hash.select()
參數:散列值
封鎖條件
返回:根據塊條件從哈希中獲取數組。
示例1:
# Ruby code for Hash.select() 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}
# select Value
puts "Hash a select form : #{a.select {|key, value| key < "b"}}\n\n"
輸出:
Hash a select form : {"a"=>100}
示例2:
# Ruby code for Hash.select() method
# declaring Hash value
b = {"a" => 100}
# declaring Hash value
c = {"a" => 100, "c" => 300, "b" => 200}
# select Value
puts "Hash b select form : #{b.select{|key, value| value < 200}}\n\n"
puts "Hash c select form : #{c.select{|key, value| key < "b"}}\n\n"
輸出:
Hash b select form : {"a"=>100} Hash c select form : {"a"=>100}
相關用法
- Ruby Hash select!()用法及代碼示例
- Ruby Hash value?用法及代碼示例
- Ruby Hash to_a用法及代碼示例
- Ruby Hash to_h用法及代碼示例
- Ruby Hash shift()用法及代碼示例
- Ruby Hash reject!用法及代碼示例
- Ruby Hash store()用法及代碼示例
- Ruby Hash size()用法及代碼示例
- Ruby Hash reject用法及代碼示例
- Ruby Hash length()用法及代碼示例
- Ruby Hash to_proc用法及代碼示例
- Ruby Hash values_at用法及代碼示例
注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 Ruby | Hash select() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。