Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/basset/core_extensions.rb

Overview

Extensions to the array class.

Instance Method Summary collapse

Instance Method Details

#pick_randomObject

Returns a random item from the array



19
20
21
# File 'lib/basset/core_extensions.rb', line 19

def pick_random
  self[rand(self.size)]
end

#randomizeObject

Returns a randomized array



24
25
26
# File 'lib/basset/core_extensions.rb', line 24

def randomize
  self.sort_by { rand }
end

#randomize!Object

Randomizes array in place



33
34
35
# File 'lib/basset/core_extensions.rb', line 33

def randomize!
  self.replace(self.randomize)
end

#restObject

Returns a new array that contains everything except the first element of this one. (just like in lisp)



9
10
11
# File 'lib/basset/core_extensions.rb', line 9

def rest
  self.slice(1, size)
end

#secondObject

Returns the second item in the array



14
15
16
# File 'lib/basset/core_extensions.rb', line 14

def second
  self[1]
end

#sumObject



28
29
30
# File 'lib/basset/core_extensions.rb', line 28

def sum
  inject(0) { |sum, val| sum + val }
end