Skip to content

Instantly share code, notes, and snippets.

@pnelligan
Created June 12, 2011 12:52
Show Gist options
  • Save pnelligan/1021514 to your computer and use it in GitHub Desktop.
Save pnelligan/1021514 to your computer and use it in GitHub Desktop.
Array divider
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