Skip to content

Instantly share code, notes, and snippets.

@johnmaxwell
Last active July 16, 2016 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnmaxwell/b77be6834150f3a6d9a03c91f2a554f0 to your computer and use it in GitHub Desktop.
Save johnmaxwell/b77be6834150f3a6d9a03c91f2a554f0 to your computer and use it in GitHub Desktop.
class Array
def bifurcate(size=length)
if size < 0
raise ArgumentError, "attempt to bifurcate using negative size"
end
[take(size), drop(size)]
end
end
('a'..'g').to_a.bifurcate(2)
# => [["a", "b"], ["c", "d", "e", "f", "g"]]
('a'..'g').to_a.bifurcate(20)
# => [["a", "b", "c", "d", "e", "f", "g"], []]
('a'..'g').to_a.bifurcate()
# => [["a", "b", "c", "d", "e", "f", "g"], []]
('a'..'g').to_a.bifurcate(0)
# [[], ["a", "b", "c", "d", "e", "f", "g"]]
('a'..'g').to_a.bifurcate(-1)
# ArgumentError: attempt to bifurcate using negative size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment