本文簡要介紹ruby語言中 Array.filter!
的用法。
用法
filter!()
別名:select!
調用塊,如果與 self
的每個元素一起給出;從 self
中刪除塊返回 false
或 nil
的那些元素。
如果刪除了任何元素,則返回 self
:
a = [:foo, 'bar', 2, :bam]
a.select! {|element| element.to_s.start_with?('b') } # => ["bar", :bam]
如果沒有刪除任何元素,則返回 nil
。
如果沒有給出塊,則返回一個新的枚舉器:
a = [:foo, 'bar', 2, :bam]
a.select! # => #<Enumerator: [:foo, "bar", 2, :bam]:select!>
Array#filter!
是 Array#select!
的別名。
相關用法
- Ruby Array.filter用法及代碼示例
- Ruby Array.fill用法及代碼示例
- Ruby Array.find_index用法及代碼示例
- Ruby Array.first用法及代碼示例
- Ruby Array.flatten!用法及代碼示例
- Ruby Array.fetch用法及代碼示例
- Ruby Array.flatten用法及代碼示例
- Ruby Array.push用法及代碼示例
- Ruby Array.hash用法及代碼示例
- Ruby Array.to_a用法及代碼示例
- Ruby Array.to_h用法及代碼示例
- Ruby Array.to_s用法及代碼示例
- Ruby Array.array + other_array用法及代碼示例
- Ruby Array.take()用法及代碼示例
- Ruby Array.reject!用法及代碼示例
- Ruby Array.reject用法及代碼示例
- Ruby Array.repeated_permutation()用法及代碼示例
- Ruby Array.index()用法及代碼示例
- Ruby Array.pack()用法及代碼示例
- Ruby Array.rassoc(obj)用法及代碼示例
- Ruby Array.values_at()用法及代碼示例
- Ruby Array.each用法及代碼示例
- Ruby Array.sort用法及代碼示例
- Ruby Array.unshift()用法及代碼示例
- Ruby Array.reverse用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Array.filter!。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。