Skip to content

Instantly share code, notes, and snippets.

@willf
Created January 24, 2012 23:09
Show Gist options
  • Save willf/1673379 to your computer and use it in GitHub Desktop.
Save willf/1673379 to your computer and use it in GitHub Desktop.
Another version of split random
class Array
# split array into groups up to n items, randomly choosing the size.
def split_random(n)
size == 0 ? self : (size == 1 ? [self] : self[1..-1].reduce([[self[0]]]){|ll, elt| (rand(n)==(n-1) || ll[-1].size==n) ? ll + [[elt]] : ll[0..-2] + [ll[-1] +[elt]]})
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment