本文簡要介紹ruby語言中 Hash.reject
的用法。
用法
reject {|key, value| ... } → new_hash
reject → new_enumerator
返回一個新的 Hash 對象,其條目是來自 self
的所有條目,塊為其返回 false
或 nil
:
h = {foo: 0, bar: 1, baz: 2}
h1 = h.reject {|key, value| key.start_with?('b') }
h1 # => {:foo=>0}
如果沒有給出塊,則返回一個新的枚舉器:
h = {foo: 0, bar: 1, baz: 2}
e = h.reject # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:reject>
h1 = e.each {|key, value| key.start_with?('b') }
h1 # => {:foo=>0}
相關用法
- Ruby Hash.reject用法及代碼示例
- Ruby Hash.reject!用法及代碼示例
- Ruby Hash.replace用法及代碼示例
- Ruby Hash.rehash用法及代碼示例
- Ruby Hash.replace()用法及代碼示例
- Ruby Hash.ruby2_keywords_hash用法及代碼示例
- Ruby Hash.rassoc(obj)用法及代碼示例
- Ruby Hash.rassoc用法及代碼示例
- Ruby Hash.ruby2_keywords_hash?用法及代碼示例
- Ruby Hash.delete()用法及代碼示例
- Ruby Hash.new用法及代碼示例
- Ruby Hash.size用法及代碼示例
- Ruby Hash.delete用法及代碼示例
- Ruby Hash.hash <=用法及代碼示例
- Ruby Hash.select!用法及代碼示例
- Ruby Hash.to_s用法及代碼示例
- Ruby Hash.values_at()用法及代碼示例
- Ruby Hash.fetch_values()用法及代碼示例
- Ruby Hash.select用法及代碼示例
- Ruby Hash.initialize_copy用法及代碼示例
- Ruby Hash.merge!用法及代碼示例
- Ruby Hash.delete_if用法及代碼示例
- Ruby Hash.slice用法及代碼示例
- Ruby Hash.hash用法及代碼示例
- Ruby Hash.each_value用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Hash.reject。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。