Skip to content
Permalink
Browse files

Merge pull request #610 from kostya/yield_multi

Enumerable yields multiple
  • Loading branch information
alex committed Apr 11, 2013
2 parents a44c29f + 82317c9 commit caa417d67ec95d952ced7ae1983a5831397aa270
Showing with 46 additions and 74 deletions.
  1. +42 −26 lib-topaz/enumerable.rb
  2. +2 −2 lib-topaz/hash.rb
  3. +0 −2 spec/tags/core/enumerable/all_tags.txt
  4. +0 −2 spec/tags/core/enumerable/any_tags.txt
  5. +0 −2 spec/tags/core/enumerable/detect_tags.txt
  6. +0 −1 spec/tags/core/enumerable/drop_while_tags.txt
  7. +0 −1 spec/tags/core/enumerable/each_cons_tags.txt
  8. +0 −1 spec/tags/core/enumerable/each_slice_tags.txt
  9. +0 −5 spec/tags/core/enumerable/each_with_index_tags.txt
  10. +0 −1 spec/tags/core/enumerable/each_with_object_tags.txt
  11. +0 −2 spec/tags/core/enumerable/entries_tags.txt
  12. +0 −2 spec/tags/core/enumerable/find_all_tags.txt
  13. +0 −2 spec/tags/core/enumerable/find_tags.txt
  14. +0 −1 spec/tags/core/enumerable/first_tags.txt
  15. +0 −1 spec/tags/core/enumerable/group_by_tags.txt
  16. +0 −1 spec/tags/core/enumerable/include_tags.txt
  17. +0 −1 spec/tags/core/enumerable/inject_tags.txt
  18. +0 −1 spec/tags/core/enumerable/max_by_tags.txt
  19. +0 −1 spec/tags/core/enumerable/member_tags.txt
  20. +0 −1 spec/tags/core/enumerable/min_by_tags.txt
  21. +0 −1 spec/tags/core/enumerable/minmax_by_tags.txt
  22. +0 −2 spec/tags/core/enumerable/none_tags.txt
  23. +0 −2 spec/tags/core/enumerable/one_tags.txt
  24. +0 −1 spec/tags/core/enumerable/partition_tags.txt
  25. +0 −1 spec/tags/core/enumerable/reduce_tags.txt
  26. +0 −1 spec/tags/core/enumerable/reject_tags.txt
  27. +0 −1 spec/tags/core/enumerable/reverse_each_tags.txt
  28. +0 −2 spec/tags/core/enumerable/select_tags.txt
  29. +0 −1 spec/tags/core/enumerable/sort_by_tags.txt
  30. +0 −1 spec/tags/core/enumerable/sort_tags.txt
  31. +0 −1 spec/tags/core/enumerable/take_tags.txt
  32. +0 −2 spec/tags/core/enumerable/to_a_tags.txt
  33. +2 −1 spec/tags/core/enumerator/each_with_index_tags.txt
  34. +0 −1 spec/tags/core/enumerator/inject_tags.txt
@@ -1,7 +1,7 @@
module Enumerable
def first(*args)
if args.empty?
self.each { |e| return e }
self.each_entry { |e| return e }
nil
else
take(*args)
@@ -44,18 +44,18 @@ def inject(*args)

alias reduce inject

def each_with_index(&block)
return self.enum_for(:each_with_index) if !block
def each_with_index(*args, &block)
return self.enum_for(:each_with_index, *args) if !block
i = 0
self.each do |obj|
self.each_entry(*args) do |obj|
yield obj, i
i += 1
end
end

def each_with_object(memo, &block)
return self.enum_for(:each_with_object, memo) unless block
self.each do |elm|
self.each_entry do |elm|
yield elm, memo
end
memo
@@ -77,23 +77,27 @@ def reverse_each(&block)
end

def all?(&block)
self.each do |obj|
return false unless (block ? block.call(obj) : obj)
if block
self.each { |*e| return false unless yield(*e) }
else
self.each_entry { |e| return false unless e }
end
true
end

def any?(&block)
self.each do |obj|
return true if (block ? block.call(obj) : obj)
if block
self.each { |*e| return true if yield(*e) }
else
self.each_entry { |e| return true if e }
end
false
end

def select(&block)
return self.enum_for(:select) unless block
result = []
self.each do |o|
self.each_entry do |o|
if block.call(o)
result << o
end
@@ -104,7 +108,7 @@ def select(&block)
alias :find_all :select

def include?(obj)
self.each do |o|
self.each_entry do |o|
return true if o == obj
end
false
@@ -123,7 +127,7 @@ def drop_while(&block)
return self.enum_for(:drop_while) if !block
result = []
dropping = true
self.each do |o|
self.each_entry do |o|
unless dropping && yield(o)
result << o
dropping = false
@@ -132,9 +136,9 @@ def drop_while(&block)
result
end

def to_a
def to_a(*args)
result = []
self.each do |i|
self.each_entry(*args) do |i|
result << i
end
result
@@ -143,7 +147,7 @@ def to_a

def detect(ifnone = nil, &block)
return self.enum_for(:detect, ifnone) unless block
self.each do |o|
self.each_entry do |o|
return o if block.call(o)
end
ifnone.is_a?(Proc) ? ifnone.call : ifnone
@@ -155,7 +159,7 @@ def take(n)
raise ArgumentError.new("attempt to take negative size") if n < 0
result = []
unless n == 0
self.each do |o|
self.each_entry do |o|
result << o
break if result.size == n
end
@@ -166,7 +170,7 @@ def take(n)
def take_while(&block)
return self.enum_for(:take_while) unless block
result = []
self.each do |o|
self.each_entry do |o|
break unless yield(o)
result << o
end
@@ -176,7 +180,7 @@ def take_while(&block)
def reject(&block)
return self.enum_for(:reject) unless block
result = []
self.each do |o|
self.each_entry do |o|
result << o unless yield(o)
end
result
@@ -241,7 +245,7 @@ def minmax_by(&block)
def partition(&block)
return self.enum_for(:partition) unless block
a, b = [], []
self.each do |e|
self.each_entry do |e|
block.call(e) ? a.push(e) : b.push(e)
end
[a, b]
@@ -264,23 +268,33 @@ def count(*args, &block)

def one?(&block)
c = 0
self.each do |e|
c += 1 if block ? yield(e) : e
if block
self.each do |*e|
c += 1 if yield(*e)
return false if c > 1
end
else
self.each_entry do |e|
c += 1 if e
return false if c > 1
end
end
c == 1
end

def none?(&block)
self.each do |e|
return false if block ? yield(e) : e
if block
self.each { |*e| return false if yield(*e) }
else
self.each_entry { |e| return false if e }
end
true
end

def group_by(&block)
return self.enum_for(:group_by) unless block
h = {}
self.each do |e|
self.each_entry do |e|
v = yield e
a = h.fetch(v) { |v| h[v] = [] }
a << e
@@ -290,8 +304,10 @@ def group_by(&block)

def find_index(obj = nil, &block)
return self.enum_for(:find_index) if !obj && !block
each_with_index do |e, i|
i = 0
each do |e|
return i if obj ? (e == obj) : block.call(e)
i += 1
end
nil
end
@@ -318,7 +334,7 @@ def each_slice(num, &block)
num = Topaz.convert_type(num, Fixnum, :to_int)
raise ArgumentError.new("invalid slice size") if num <= 0
buf = []
self.each do |e|
self.each_entry do |e|
buf << e
if buf.size == num
yield buf
@@ -119,11 +119,11 @@ def invert
def inspect
result = "{"
recursion = Thread.current.recursion_guard(:hash_inspect, self) do
self.each_with_index do |key, i|
self.each_with_index do |(key, value), i|
if i > 0
result << ", "
end
result << "#{key.inspect}=>#{self[key].inspect}"
result << "#{key.inspect}=>#{value.inspect}"
end
end
if recursion

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

@@ -1,2 +1 @@
fails:Enumerable#inject can take a symbol argument
fails:Enumerable#inject gathers whole arrays as elements when each yields multiple

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

@@ -1,2 +1 @@
fails:Enumerable#reduce can take a symbol argument
fails:Enumerable#reduce gathers whole arrays as elements when each yields multiple

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

@@ -1,2 +1 @@
fails:Enumerable#sort compare values returned by block with 0
fails:Enumerable#sort gathers whole arrays as elements when each yields multiple

This file was deleted.

This file was deleted.

@@ -1,4 +1,5 @@
fails:Enumerator#each_with_index returns an enumerator if no block is supplied
fails:Enumerator#each_with_index passes on the given block's return value
fails:Enumerator#each_with_index returns the iterator's return value
fails:Enumerator#each_with_index returns the correct value if chained with itself
fails:Enumerator#each_with_index returns the object being enumerated when given a block
fails:Enumerator#each_with_index raises an ArgumentError if passed extra arguments
@@ -1,3 +1,2 @@
fails:Enumerator#inject calls #each on the underlying object until it's exhausted
fails:Enumerator#inject calls the method given in the constructor until it's exhausted
fails:Enumerator#inject works when chained against each_with_index

0 comments on commit caa417d

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