Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/magician/array.rb

Overview

Magician’s extensions to the Array class.

Instance Method Summary collapse

Instance Method Details

#meanFloat Also known as: average

Gets the mean (average) of the numbers in the array. The mean of an array with no numbers is nil.

Returns:

  • (Float)

    the mean (average) of the numbers in the array



47
48
49
50
51
# File 'lib/magician/array.rb', line 47

def mean
	nums = numerics
	return nil if nums.empty?
	nums.sum.to_f / nums.size
end

#medianNumeric

Gets the median of the numbers in the array (the value in the middle of a sorted version of the array, numbers only). If the array has an even number of numbers, the middle two numbers will be averaged. The median of an array with no numbers is nil.

Returns:

  • (Numeric)

    the median of the numbers in the array



59
60
61
62
63
64
65
66
67
68
# File 'lib/magician/array.rb', line 59

def median
	nums = numerics
	return nil if nums.empty?
	sorted = nums.sort
	if sorted.length.odd?
		nums[nums.length/2]
	else
		(nums[nums.length/2-1] + nums[nums.length/2]) / 2.0
	end
end

#modeArray

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.

most frequently (they must all have the same number of occurrences)

Returns:

  • (Array)

    an array of all of the items in the array that occur the



75
76
77
78
79
80
# File 'lib/magician/array.rb', line 75

def mode
	return nil if empty?
	occ = occurences
	max_occ = occ.values.max
	occ.select { |key, value| value == max_occ }.keys
end

#numericsArray

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.

Returns:

  • (Array)

    a new array containing only Numerics



9
10
11
# File 'lib/magician/array.rb', line 9

def numerics
	select { |item| item.class <= Numeric }
end

#occurencesHash

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.

array

Returns:

  • (Hash)

    a hash table of the occurrences of each item from the original



89
90
91
92
93
94
# File 'lib/magician/array.rb', line 89

def occurences
	occurences = {}
	each { |item| occurences[item] = 0 }
	each { |item| occurences[item] += 1 }
	occurences
end

#productNumeric

Gets the product of the numbers in the array. The product of an array with no numbers is 1.

Returns:

  • (Numeric)

    the product of the numbers in the array



27
28
29
30
31
# File 'lib/magician/array.rb', line 27

def product
	nums = numerics
	return 1 if nums.empty?
	nums.inject(:*)
end

#rangeNumeric

Gets the range of the numbers in the array (maximum - minimum). The range of an array with no numbers is nil.

Returns:

  • (Numeric)

    the range of the numbers in the array



37
38
39
40
41
# File 'lib/magician/array.rb', line 37

def range
	nums = numerics
	return nil if nums.empty?
	nums.max - nums.min
end

#sumNumeric

Gets the sum of the numbers in the array. The sum of an array with no numbers is 0.

Returns:

  • (Numeric)

    the sum of the numbers in the array



17
18
19
20
21
# File 'lib/magician/array.rb', line 17

def sum
	nums = numerics
	return 0 if nums.empty?
	nums.inject(:+)
end