Created
August 12, 2018 20:48
-
-
Save calrrox/a243d25ca1aad6321784abe77f49ed36 to your computer and use it in GitHub Desktop.
sample extension, create sample! method
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 sample!(n = nil) | |
raise TypeError.new('no implicit conversion of String into Integer') unless n.class == Integer | |
new_array = self.clone | |
random_sample = n.nil? ? sample : sample(n) | |
new_array -= random_sample.class == Array ? random_sample : [random_sample] | |
replace(new_array) | |
random_sample | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment