Module: Enumerable
- Defined in:
- lib/rand.rb
Instance Method Summary collapse
-
#each_random(&block) ⇒ Object
Calls block once for each element in self in random order, passing that element as a parameter.
-
#map_random(&block) ⇒ Object
Invokes block once for each element of self in random order.
-
#pick ⇒ Object
Choose and return a random element of the Enumerable.
-
#shuffle ⇒ Object
Return an array of the elements in random order.
Instance Method Details
#each_random(&block) ⇒ Object
Calls block once for each element in self in random order, passing that element as a parameter.
24 25 26 |
# File 'lib/rand.rb', line 24 def each_random(&block) shuffle.each(&block) end |
#map_random(&block) ⇒ Object
Invokes block once for each element of self in random order. Creates a new array containing the values returned by the block.
30 31 32 |
# File 'lib/rand.rb', line 30 def map_random(&block) shuffle.map(&block) end |
#pick ⇒ Object
Choose and return a random element of the Enumerable.
[1, 2, 3, 4].pick #=> 2 (or 1, 3, 4)
12 13 14 |
# File 'lib/rand.rb', line 12 def pick entries.pick end |
#shuffle ⇒ Object
Return an array of the elements in random order.
[1, 2, 3, 4].shuffle #=> [3, 4, 1, 2]
18 19 20 |
# File 'lib/rand.rb', line 18 def shuffle entries.shuffle end |