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.
25 26 27 |
# File 'lib/rand.rb', line 25 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.
31 32 33 |
# File 'lib/rand.rb', line 31 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)
13 14 15 |
# File 'lib/rand.rb', line 13 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]
19 20 21 |
# File 'lib/rand.rb', line 19 def shuffle entries.shuffle end |