Created
May 2, 2011 08:37
-
-
Save masarakki/951311 to your computer and use it in GitHub Desktop.
add method to Integer to choice integers like array#sample
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 Integer | |
def sample(num = 1) | |
to_a.sample(num) | |
end | |
def to_a | |
@to_a ||= self.times.map{|i| i + 1} | |
end | |
end | |
# | |
# 10.sample(2) | |
# #=> [5, 1] | |
# | |
# * choice integers from [1, 2, ... n] | |
# * sampled integers are unique | |
# | |
# it make easy to generate mock like that | |
# 100.times do | |
# article = article.new(...) | |
# 100.sample(10).each do |tag_id| | |
# article.tag_ids << tag_id | |
# end | |
# end | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment