Created
March 15, 2015 10:03
-
-
Save dd1994/0e80f60046cf48434c69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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