Created
June 12, 2011 12:52
-
-
Save pnelligan/1021514 to your computer and use it in GitHub Desktop.
Array divider
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 | |
def divide_by n | |
return_array = [] | |
inner_array = [] | |
index = 0 | |
self.each do |array_item| | |
if index % n == 0 && index > 0 | |
return_array << inner_array | |
inner_array = [] | |
end | |
inner_array << array_item | |
return_array << inner_array if index == self.size - 1 | |
index += 1 | |
end | |
return_array | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment