class Array

Magician's extensions to the Array class.

Public Instance Methods

average()

Alias average to mean.

Alias for: mean
mean() click to toggle source

Gets the mean (average) of the elements of the array. The mean of an empty array is nil. The array must only contain Numerics or a RuntimeError will be raised.

@return [Float, nil] the mean (average) of the elements of the array

@raise [RuntimeError] if the Array contains non-Numeric objects

# File lib/magician/array.rb, line 73
def mean
  require_numerics

  empty? ? nil : sum.to_f / length
end
Also aliased as: average
median() click to toggle source

Sorts the array and finds the element in the middle. The exact same functionality can be achieved by sorting the array and then running the middle method on it. The array must only contain Numerics or a RuntimeError will be raised.

@see middle

@return [Numeric, nil] the median of the elements of the array

@raise [RuntimeError] if the Array contains non-Numeric objects

# File lib/magician/array.rb, line 89
def median
  require_numerics

  sort.middle
end
middle() click to toggle source

Finds the middle element of the array. If the array has an even number of elements, the middle two elements will be averaged. The middle of an empty array is nil. The array must only contain Numerics or a RuntimeError will be raised.

@return [Numeric, nil] the middle of the elements of the array

@raise [RuntimeError] if the Array contains non-Numeric objects

# File lib/magician/array.rb, line 45
def middle
  require_numerics
  return nil if empty?

  middle_index = length / 2
  length.odd? ? slice(middle_index) : [slice(middle_index-1), slice(middle_index)].mean
end
mode() click to toggle source

Gets the mode(s) of the items in the array (the item(s) that occur(s) most frequently). The mode of an empty array is nil.

@return [Array, nil] an array of all of the items in the array that occur

the most frequently (they must all have the same number of occurrences)
# File lib/magician/array.rb, line 100
def mode
  return nil if empty?

  occ = occurences
  max_occ = occ.values.max

  occ.select { |key, value| value == max_occ }.keys
end
numerics() click to toggle source

Returns all numbers from the array, in order. This is done by choosing all objects from the array that are instances of Numeric or one of its subclasses.

@return [Array] a new array containing only Numerics

# File lib/magician/array.rb, line 9
def numerics
  select { |item| item.class <= Numeric }
end
occurences() click to toggle source

Gets a hash table with the number of occurrences of each item from the original array. The keys are the items from the original array, and the values are integers counting the number of occurrences of the associated key values.

@return [Hash] a hash table of the occurrences of each item from the original

array
# File lib/magician/array.rb, line 116
def occurences
  occurences = Hash.new 0
  each { |item| occurences[item] += 1 }
  occurences
end
product() click to toggle source

Gets the product of the array elements. The product of an empty array is 1. The array must only contain Numerics or a RuntimeError will be raised.

@return [Numeric] the product of the elements of the array

@raise [RuntimeError] if the Array contains non-Numeric objects

# File lib/magician/array.rb, line 31
def product
  require_numerics

  empty? ? 1 : reduce(:*)
end
range() click to toggle source

Gets the range of the elements of the array (maximum - minimum). The range of an empty array is nil. The array must only contain Numerics or a RuntimeError will be raised.

@return [Numeric, nil] the range of the elements of the array

@raise [RuntimeError] if the Array contains non-Numeric objects

# File lib/magician/array.rb, line 60
def range
  require_numerics

  empty? ? nil : max - min
end
sum() click to toggle source

Gets the sum of the array elements. The sum of an empty array is 0. The array must only contain Numerics or a RuntimeError will be raised.

@return [Numeric] the sum of the elements of the array

@raise [RuntimeError] if the Array contains non-Numeric objects

# File lib/magician/array.rb, line 19
def sum
  require_numerics

  empty? ? 0 : reduce(:+)
end

Private Instance Methods

require_numerics() click to toggle source

Requires that all objects in the Array are instances of Numeric (or one of its subclasses), and raises an error if they are not.

@raise [RuntimeError] if the Array contains non-Numeric objects

# File lib/magician/array.rb, line 131
def require_numerics
  unless all? { |item| item.class <= Numeric }
    calling_method = caller[0][/`.*'/][1..-2]
    raise RuntimeError, "Array##{calling_method} requires that the Array only contains Numeric objects."
  end
end