Skip to content

Instantly share code, notes, and snippets.

@calrrox
Created August 12, 2018 20:48
Show Gist options
  • Save calrrox/a243d25ca1aad6321784abe77f49ed36 to your computer and use it in GitHub Desktop.
Save calrrox/a243d25ca1aad6321784abe77f49ed36 to your computer and use it in GitHub Desktop.
sample extension, create sample! method
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