Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/basset/core_extensions.rb
Overview
Extensions to the array class.
Instance Method Summary collapse
-
#pick_random ⇒ Object
Returns a random item from the array.
-
#randomize ⇒ Object
Returns a randomized array.
-
#randomize! ⇒ Object
Randomizes array in place.
-
#rest ⇒ Object
Returns a new array that contains everything except the first element of this one.
-
#second ⇒ Object
Returns the second item in the array.
- #sum ⇒ Object
Instance Method Details
#pick_random ⇒ Object
Returns a random item from the array
20 21 22 |
# File 'lib/basset/core_extensions.rb', line 20 def pick_random self[rand(self.size)] end |
#randomize ⇒ Object
Returns a randomized array
25 26 27 |
# File 'lib/basset/core_extensions.rb', line 25 def randomize self.sort_by { rand } end |
#randomize! ⇒ Object
Randomizes array in place
34 35 36 |
# File 'lib/basset/core_extensions.rb', line 34 def randomize! self.replace(self.randomize) end |
#rest ⇒ Object
Returns a new array that contains everything except the first element of this one. (just like in lisp)
9 10 11 12 |
# File 'lib/basset/core_extensions.rb', line 9 def rest return self if empty? self.slice(1, size) end |
#second ⇒ Object
Returns the second item in the array
15 16 17 |
# File 'lib/basset/core_extensions.rb', line 15 def second self[1] end |
#sum ⇒ Object
29 30 31 |
# File 'lib/basset/core_extensions.rb', line 29 def sum inject(0) { |sum, val| sum + val } end |