Created
May 22, 2013 20:02
-
-
Save chris-79/5630453 to your computer and use it in GitHub Desktop.
Split a Ruby array into two. from: http://heyrod.com/snippet/s/split-ruby-array-in-half.html
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
a = [1,2,3,4,5] | |
# 2 new arrays | |
left,right = a.each_slice( (a.size/2.0).round ).to_a | |
# left == [1, 2, 3] | |
# right == [4, 5] | |
# make array 2-dimensional | |
a.each_slice( (a.size/2.0).round ).to_a | |
# a == [[1, 2, 3], [4, 5]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment