Skip to content
Permalink
Browse files

Merge pull request #590 from kostya/enum_eachs

enumerable#each_cons, each_slice
  • Loading branch information
alex committed Apr 10, 2013
2 parents 1f2d98d + fa3f50e commit 68a1590f278473d0fa2bcf28c7f36df8a56a4da0
@@ -272,4 +272,37 @@ def find_index(obj = nil, &block)
end
nil
end

def each_cons(num, &block)
return self.enum_for(:each_cons, num) if !block
num = Topaz.convert_type(num, Fixnum, :to_int)
raise ArgumentError.new("invalid size") if num <= 0
buf = []
self.each_with_index do |e, i|
buf << e
if i == num - 1
yield buf.dup
elsif i >= num
buf.shift
yield buf.dup
end
end
nil
end

def each_slice(num, &block)
return self.enum_for(:each_slice, num) if !block
num = Topaz.convert_type(num, Fixnum, :to_int)
raise ArgumentError.new("invalid slice size") if num <= 0
buf = []
self.each do |e|
buf << e
if buf.size == num
yield buf
buf = []
end
end
yield buf unless buf.empty?
nil
end
end
@@ -1,7 +1 @@
fails:Enumerable#each_cons passes element groups to the block
fails:Enumerable#each_cons raises an Argument Error if there is not a single parameter > 0
fails:Enumerable#each_cons tries to convert n to an Integer using #to_int
fails:Enumerable#each_cons works when n is >= full length
fails:Enumerable#each_cons yields only as much as needed
fails:Enumerable#each_cons returns an enumerator if no block
fails:Enumerable#each_cons gathers whole arrays as elements when each yields multiple
@@ -1,7 +1 @@
fails:Enumerable#each_slice passes element groups to the block
fails:Enumerable#each_slice raises an Argument Error if there is not a single parameter > 0
fails:Enumerable#each_slice tries to convert n to an Integer using #to_int
fails:Enumerable#each_slice works when n is >= full length
fails:Enumerable#each_slice yields only as much as needed
fails:Enumerable#each_slice returns an enumerator if no block
fails:Enumerable#each_slice gathers whole arrays as elements when each yields multiple

0 comments on commit 68a1590

Please sign in to comment.