Skip to content

Instantly share code, notes, and snippets.

@masarakki
Created May 2, 2011 08:37
Show Gist options
  • Save masarakki/951311 to your computer and use it in GitHub Desktop.
Save masarakki/951311 to your computer and use it in GitHub Desktop.
add method to Integer to choice integers like array#sample
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