Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/magician/array.rb
Overview
Magician’s extensions to the Array class.
Instance Method Summary collapse
-
#mean ⇒ Float
(also: #average)
Gets the mean (average) of the numbers in the array.
-
#median ⇒ Numeric
Gets the median of the numbers in the array (the value in the middle of a sorted version of the array, numbers only).
-
#mode ⇒ Array
Gets the mode(s) of the items in the array (the item(s) that occur(s) most frequently).
-
#numerics ⇒ Array
Returns all numbers from the array, in order.
-
#occurences ⇒ Hash
Gets a hash table with the number of occurrences of each item from the original array.
-
#product ⇒ Numeric
Gets the product of the numbers in the array.
-
#range ⇒ Numeric
Gets the range of the numbers in the array (maximum - minimum).
-
#sum ⇒ Numeric
Gets the sum of the numbers in the array.
Instance Method Details
#mean ⇒ Float Also known as: average
Gets the mean (average) of the numbers in the array. The mean of an array with no numbers is nil.
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 |
#median ⇒ Numeric
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.
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 |
#mode ⇒ Array
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)
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 |
#numerics ⇒ Array
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.
9 10 11 |
# File 'lib/magician/array.rb', line 9 def numerics select { |item| item.class <= Numeric } end |
#occurences ⇒ Hash
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
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 |
#product ⇒ Numeric
Gets the product of the numbers in the array. The product of an array with no numbers is 1.
27 28 29 30 31 |
# File 'lib/magician/array.rb', line 27 def product nums = numerics return 1 if nums.empty? nums.inject(:*) end |
#range ⇒ Numeric
Gets the range of the numbers in the array (maximum - minimum). The range of an array with no numbers is nil.
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 |
#sum ⇒ Numeric
Gets the sum of the numbers in the array. The sum of an array with no numbers is 0.
17 18 19 20 21 |
# File 'lib/magician/array.rb', line 17 def sum nums = numerics return 0 if nums.empty? nums.inject(:+) end |