Skip to content

Instantly share code, notes, and snippets.

@dd1994
Created March 15, 2015 10:03
Show Gist options
  • Save dd1994/0e80f60046cf48434c69 to your computer and use it in GitHub Desktop.
Save dd1994/0e80f60046cf48434c69 to your computer and use it in GitHub Desktop.
module Enumerable
def i_each_slice(n, &block)
raise "invalid slice size" if n <= 0
i, ary = 0, []
each do |a|
ary << a
i += 1
if i >= n
yield ary
i, ary = 0, []
end
end
yield ary if i > 0
end
end
(1..10).i_each_slice(3) { |a| p a }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment