Skip to content
Permalink
Browse files

Merge pull request #498 from rlevick/enumerable#drop_while

Add Enumerable#drop_while
  • Loading branch information
alex committed Mar 12, 2013
2 parents 743a143 + edf1d00 commit 26d7f3cfa9e58de1d9d5f1b6a19dc21dc07ddfcc
Showing with 31 additions and 13 deletions.
  1. +1 −0 AUTHORS.rst
  2. +2 −2 lib-topaz/array.rb
  3. +18 −6 lib-topaz/enumerable.rb
  4. +0 −5 spec/tags/core/enumerable/drop_while_tags.txt
  5. +10 −0 tests/modules/test_enumerable.py
@@ -19,3 +19,4 @@ Authors
* Armin Rigo
* Robin Schreiber
* Jeremy Thurgood
* Ryan Levick
@@ -256,10 +256,10 @@ def max(&block)
def uniq!(&block)
raise RuntimeError.new("can't modify frozen #{self.class}") if frozen?
seen = {}
old_len = self.length
old_length = self.length
i = 0
shifted = 0
while i < self.length do
while i < old_length do
item = self[i]
item = yield(item) if block
if seen.include? item
@@ -41,13 +41,13 @@ def any?(&block)
end

def select(&block)
ary = []
result = []
self.each do |o|
if block.call(o)
ary << o
result << o
end
end
ary
result
end

def include?(obj)
@@ -59,9 +59,21 @@ def include?(obj)

def drop(n)
raise ArgumentError.new("attempt to drop negative size") if n < 0
ary = self.to_a
return [] if n > ary.size
ary[n...ary.size]
result = self.to_a
return [] if n > result.size
result[n...result.size]
end

def drop_while(&block)
result = []
dropping = true
self.each do |o|
unless dropping && yield(o)
result << o
dropping = false
end
end
result
end

def to_a
@@ -1,7 +1,2 @@
fails:Enumerable#drop_while returns an Enumerator if no block given
fails:Enumerable#drop_while returns no/all elements for {true/false} block
fails:Enumerable#drop_while accepts returns other than true/false
fails:Enumerable#drop_while passes elements to the block until the first false
fails:Enumerable#drop_while will only go through what's needed
fails:Enumerable#drop_while doesn't return self when it could
fails:Enumerable#drop_while gathers whole arrays as elements when each yields multiple
@@ -107,6 +107,16 @@ def test_drop(self, space):
with self.raises(space, "ArgumentError", 'attempt to drop negative size'):
space.execute("""return [0,1,2,3,4,5,6,7].drop -3""")

def test_drop_while(self, space):
w_res = space.execute("""return [1, 2, 3, 4, 5, 0].drop_while { |i| i < 3 }""")
assert self.unwrap(space, w_res) == [3, 4, 5, 0]

w_res = space.execute("""return [1, 2, 3].drop_while { |i| i == 0 } """)
assert self.unwrap(space, w_res) == [1, 2, 3]

w_res = space.execute("""return [].drop_while { |i| i > 3 }""")
assert self.unwrap(space, w_res) == []

def test_to_a(self, space):
w_res = space.execute("""return (5..10).to_a""")
assert self.unwrap(space, w_res) == [x for x in range(5, 11)]

0 comments on commit 26d7f3c

Please sign in to comment.
You can’t perform that action at this time.