Skip to content
Permalink
Browse files

Merge pull request #500 from rlevick/enumerable

Enumerable#take, Enumerable#take_while, Enumerable#reject
  • Loading branch information
alex committed Mar 12, 2013
2 parents 885156d + aa86a7c commit 04d2f91cc2b3d81cc7c43e359567986ae615cd7c
@@ -56,6 +56,7 @@ def include?(obj)
end
false
end
alias member? include?

def drop(n)
raise ArgumentError.new("attempt to drop negative size") if n < 0
@@ -83,6 +84,7 @@ def to_a
end
result
end
alias entries to_a

def detect(ifnone = nil, &block)
self.each do |o|
@@ -91,4 +93,33 @@ def detect(ifnone = nil, &block)
return ifnone
end
alias find detect

def take(n)
raise ArgumentError.new("attempt to take negative size") if n < 0
result = []
unless n == 0
self.each do |o|
result << o
break if result.size == n
end
end
result
end

def take_while(&block)
result = []
self.each do |o|
break unless yield(o)
result << o
end
result
end

def reject(&block)
result = []
self.each do |o|
result << o unless yield(o)
end
result
end
end
@@ -1,3 +1,2 @@
fails:Enumerable#entries returns an array containing the elements
fails:Enumerable#entries passes through the values yielded by #each_with_index
fails:Enumerable#entries passes arguments to each
@@ -1,4 +1 @@
fails:Enumerable#member? returns true if any element == argument for numbers
fails:Enumerable#member? returns true if any element == argument for other objects
fails:Enumerable#member? returns true if any member of enum equals obj when == compare different classes (legacy rubycon)
fails:Enumerable#member? gathers whole arrays as elements when each yields multiple
@@ -1,3 +1,2 @@
fails:Enumerable#reject returns an array of the elements for which block is false
fails:Enumerable#reject returns an Enumerator if called without a block
fails:Enumerable#reject gathers whole arrays as elements when each yields multiple
@@ -1,11 +1,3 @@
fails:Enumerable#take requires an argument
fails:Enumerable#take when passed an argument returns the first count elements if given a count
fails:Enumerable#take when passed an argument returns an empty array when passed count on an empty array
fails:Enumerable#take when passed an argument returns an empty array when passed count == 0
fails:Enumerable#take when passed an argument returns an array containing the first element when passed count == 1
fails:Enumerable#take when passed an argument raises an ArgumentError when count is negative
fails:Enumerable#take when passed an argument returns the entire array when count > length
fails:Enumerable#take when passed an argument tries to convert the passed argument to an Integer using #to_int
fails:Enumerable#take when passed an argument raises a TypeError if the passed argument is not numeric
fails:Enumerable#take when passed an argument gathers whole arrays as elements when each yields multiple
fails:Enumerable#take when passed an argument consumes only what is needed
@@ -1,6 +1 @@
fails:Enumerable#take_while returns an Enumerator if no block given
fails:Enumerable#take_while returns no/all elements for {true/false} block
fails:Enumerable#take_while accepts returns other than true/false
fails:Enumerable#take_while passes elements to the block until the first false
fails:Enumerable#take_while will only go through what's needed
fails:Enumerable#take_while doesn't return self when it could
@@ -152,3 +152,31 @@ def test_map(self, space):
assert self.unwrap(space, w_res) == [x for x in range(2, 7)]
w_res = space.execute("return [1,2,3,4,5].collect { |i| i + 1 }")
assert self.unwrap(space, w_res) == [x for x in range(2, 7)]

def test_take(self, space):
w_res = space.execute("return [1,2,3,4,5].take(2)")
assert self.unwrap(space, w_res) == [1, 2]
w_res = space.execute("return [1,2,3,4,5].take(0)")
assert self.unwrap(space, w_res) == []
w_res = space.execute("return [].take(2)")
assert self.unwrap(space, w_res) == []

def test_take_while(self, space):
w_res = space.execute("return (1..10).take_while { |i| i < 4 }")
assert self.unwrap(space, w_res) == [1, 2, 3]
w_res = space.execute("return [].take_while { |i| i == 11 }")
assert self.unwrap(space, w_res) == []
w_res = space.execute("return (1..10).take_while { |i| i > 11 }")
assert self.unwrap(space, w_res) == []
w_res = space.execute("return (1..10).take_while { |i| i < 11 }")
assert self.unwrap(space, w_res) == [x for x in range(1, 11)]

def test_reject(self, space):
w_res = space.execute("return [1, 2, 3].reject { |i| i == 3 }")
assert self.unwrap(space, w_res) == [1, 2]
w_res = space.execute("return [].reject { |i| i == 3 }")
assert self.unwrap(space, w_res) == []
w_res = space.execute("return (1..10).reject { |i| i > 11 }")
assert self.unwrap(space, w_res) == [x for x in range(1, 11)]
w_res = space.execute("return (1..10).reject { |i| i < 11 }")
assert self.unwrap(space, w_res) == []

0 comments on commit 04d2f91

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