Skip to content

Instantly share code, notes, and snippets.

@SteveBenner
Last active December 17, 2015 21:39
Show Gist options
  • Save SteveBenner/5676567 to your computer and use it in GitHub Desktop.
Save SteveBenner/5676567 to your computer and use it in GitHub Desktop.
Divide an array into equal halves
# @return [Array] Two-member Array containing self evenly split into two sub-arrays
# @example
# [1,2,3,4].halves #=> [[1,2],[3,4]]
# [1,2,3].halves #=> [[1,2],[3]]
def halves
[self[0..(self.size/2.0).round-1], self[(self.size/2.0).round..self.size]]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment