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



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

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

#randomizeObject

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

#restObject

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

#secondObject

Returns the second item in the array



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

def second
  self[1]
end

#sumObject



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

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