Created
September 9, 2013 05:28
-
-
Save baya/6491811 to your computer and use it in GitHub Desktop.
rand find from a collection
This file contains hidden or 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
def rand_find(num=1, &p) | |
scope = p.call | |
items = [] | |
rand_numbers = (0...scope.count).to_a | |
num.times do | |
break if rand_numbers.size == 0 | |
rand_number = rand_numbers.delete_at(rand rand_numbers.size) | |
items << scope[rand_number] | |
end | |
items | |
end | |
rand_find(8) do | |
User.where(status: 0) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment