本文簡要介紹ruby語言中 Array.take_while
的用法。
用法
take_while {|element| ... } → new_array
take_while → new_enumerator
返回一個包含零個或多個 self
的前導元素的新數組;不修改 self
。
給定一個塊,調用具有 self
的每個連續元素的塊;如果塊返回 false
或 nil
則停止;返回一個新的 Array
,其中包含塊為其返回真值的元素:
a = [0, 1, 2, 3, 4, 5]
a.take_while {|element| element < 3 } # => [0, 1, 2]
a.take_while {|element| true } # => [0, 1, 2, 3, 4, 5]
a # => [0, 1, 2, 3, 4, 5]
在沒有給出塊的情況下,返回一個新的 Enumerator:
[0, 1].take_while # => #<Enumerator: [0, 1]:take_while>
相關用法
- Ruby Array.take_while用法及代碼示例
- Ruby Array.take()用法及代碼示例
- Ruby Array.take用法及代碼示例
- Ruby Array.to_a用法及代碼示例
- Ruby Array.to_h用法及代碼示例
- Ruby Array.to_s用法及代碼示例
- Ruby Array.transpose用法及代碼示例
- Ruby Array.push用法及代碼示例
- Ruby Array.hash用法及代碼示例
- Ruby Array.array + other_array用法及代碼示例
- Ruby Array.reject!用法及代碼示例
- Ruby Array.flatten!用法及代碼示例
- 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.fetch用法及代碼示例
- Ruby Array.flatten用法及代碼示例
- Ruby Array.sort用法及代碼示例
- Ruby Array.unshift()用法及代碼示例
- Ruby Array.reverse用法及代碼示例
- Ruby Array.array | other_array用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Array.take_while。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。