Skip to content

Instantly share code, notes, and snippets.

@james
Created November 21, 2008 16:16
Show Gist options
  • Save james/27479 to your computer and use it in GitHub Desktop.
Save james/27479 to your computer and use it in GitHub Desktop.
class Array
def split_into(n)
(0...(s = (self.size.to_f/n).ceil)-1).inject([]) { |m, i| m << self.slice(s * i, s); m }
end
end
class Array
def split_into(n)
size = (self.size.to_f/n).ceil
result = []
n.times do |x|
result << self.slice(size * x, size)
end
result
end
end
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18].split_into(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment