Created
January 27, 2011 02:38
-
-
Save armstrjare/797962 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
class Array | |
# Splits or iterates over the array in groups of size +number+, | |
# padding any remaining slots with +fill_with+ unless it is +false+. | |
# | |
# %w(1 2 3 4 5 6 7).in_groups_of(3) {|group| p group} | |
# ["1", "2", "3"] | |
# ["4", "5", "6"] | |
# ["7", nil, nil] | |
# | |
# %w(1 2 3).in_groups_of(2, ' ') {|group| p group} | |
# ["1", "2"] | |
# ["3", " "] | |
# | |
# %w(1 2 3).in_groups_of(2, false) {|group| p group} | |
# ["1", "2"] | |
# ["3"] | |
def in_groups_of(number, fill_with = nil) | |
if fill_with == false | |
collection = self | |
else | |
# size % number gives how many extra we have; | |
# subtracting from number gives how many to add; | |
# modulo number ensures we don't add group of just fill. | |
padding = (number - size % number) % number | |
collection = dup.concat([fill_with] * padding) | |
end | |
if block_given? | |
collection.each_slice(number) { |slice| yield(slice) } | |
else | |
groups = [] | |
collection.each_slice(number) { |group| groups << group } | |
groups | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment