當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Ruby Hash select!()用法及代碼示例


Hash#select!()是一個Hash類方法,它根據塊條件檢查是否存在來自哈希函數的數組。

用法:Hash.select!()

參數:散列值
封鎖條件


返回:基於塊條件存在來自哈希的數組,否則返回false

範例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:

Hash c select! form:{"a"=>100}



相關用法


注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 Ruby | Hash select!() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。