mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
add Enumerable#drop_while
This commit is contained in:
@@ -20,5 +20,25 @@ module Enumerable
|
||||
self.each {|e| n == 0 ? ary << e : n -= 1 }
|
||||
ary
|
||||
end
|
||||
|
||||
##
|
||||
# call-seq:
|
||||
# enum.drop_while {|arr| block } -> array
|
||||
#
|
||||
# Drops elements up to, but not including, the first element for
|
||||
# which the block returns +nil+ or +false+ and returns an array
|
||||
# containing the remaining elements.
|
||||
#
|
||||
# a = [1, 2, 3, 4, 5, 0]
|
||||
# a.drop_while {|i| i < 3 } #=> [3, 4, 5, 0]
|
||||
|
||||
def drop_while(&block)
|
||||
ary, state = [], false
|
||||
self.each do |e|
|
||||
state = true if !state and !block.call(e)
|
||||
ary << e if state
|
||||
end
|
||||
ary
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -8,3 +8,8 @@ assert("Enumrable#drop") do
|
||||
assert_equal a.drop(6), []
|
||||
end
|
||||
|
||||
assert("Enumrable#drop_while") do
|
||||
a = [1, 2, 3, 4, 5, 0]
|
||||
assert_equal a.drop_while {|i| i < 3 }, [3, 4, 5, 0]
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user