Last active
July 16, 2016 14:01
-
-
Save johnmaxwell/b77be6834150f3a6d9a03c91f2a554f0 to your computer and use it in GitHub Desktop.
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
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