Module: Enumerable

Included in:
String
Defined in:
lib/magician/enumerable.rb

Overview

Magician's extensions to the Enumerable module.

Instance Method Summary collapse

Instance Method Details

#empty?Boolean

Returns true if the Enumerable contains no elements. This works by converting the Enumerable to an array and then checking if that is empty.

Returns:

  • (Boolean)

    true if the Enumerable is empty


152
153
154
# File 'lib/magician/enumerable.rb', line 152

def empty?
  to_a.empty?
end

#lengthFixnum Also known as: size

Gets the number of elements in the Enumerable. This works by converting the Enumerable to an array and then getting its length.

Returns:

  • (Fixnum)

    the length of the Enumerable


144
145
146
# File 'lib/magician/enumerable.rb', line 144

def length
  to_a.length
end

#meanFloat? Also known as: average

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

Returns:

  • (Float, nil)

    the mean (average) of the elements of the Enumerable

Raises:

  • (RuntimeError)

    if the Enumerable contains non-Numeric objects


78
79
80
81
82
# File 'lib/magician/enumerable.rb', line 78

def mean
  require_numerics

  empty? ? nil : sum.to_f / length
end

#medianNumeric?

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

Returns:

  • (Numeric, nil)

    the median of the elements of the Enumerable

Raises:

  • (RuntimeError)

    if the Enumerable contains non-Numeric objects

See Also:


94
95
96
97
98
# File 'lib/magician/enumerable.rb', line 94

def median
  require_numerics

  sort.middle
end

#middleNumeric?

Finds the middle element of the Enumerable. If the Enumerable has an even number of elements, the middle two elements will be averaged. The middle of an empty Enumerable is nil. Note that this method relies on a working to_a method for this Enumerable. The Enumerable must only contain Numerics or a RuntimeError will be raised.

Returns:

  • (Numeric, nil)

    the middle of the elements of the Enumerable

Raises:

  • (RuntimeError)

    if the Enumerable contains non-Numeric objects


48
49
50
51
52
53
54
55
56
# File 'lib/magician/enumerable.rb', line 48

def middle
  require_numerics
  return nil if empty?

  array = to_a
  middle_index = array.length / 2

  length.odd? ? array[middle_index] : [array[middle_index-1], array[middle_index]].mean
end

#modeArray?

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

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

Returns:

  • (Array, nil)

    an array of all of the items in the Enumerable that


106
107
108
109
110
111
112
113
# File 'lib/magician/enumerable.rb', line 106

def mode
  return nil if empty?

  occ = occurences
  max_occ = occ.values.max

  occ.select { |_, value| value == max_occ }.keys
end

#none?(&block) ⇒ Boolean

Returns:

  • (Boolean)

156
157
158
159
# File 'lib/magician/enumerable.rb', line 156

def none?(&block)
  block ||= lambda { |obj| obj }
  not any? &block
end

#numeric_productNumeric

Gets the (numeric) product of the Enumerable's elements. The product of an empty Enumerable is 1. The Enumerable must only contain Numerics or a RuntimeError will be raised.

Returns:

  • (Numeric)

    the product of the elements of the Enumerable

Raises:

  • (RuntimeError)

    if the Enumerable contains non-Numeric objects


33
34
35
36
37
# File 'lib/magician/enumerable.rb', line 33

def numeric_product
  require_numerics

  empty? ? 1 : reduce(:*)
end

#numericsArray

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

Returns:

  • (Array)

    an array containing all of the Numerics from the Enumerable


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

def numerics
  select { |item| item.is_a? Numeric }
end

#occurencesHash

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

original Enumerable

Returns:

  • (Hash)

    a hash table of the occurrences of each item from the


122
123
124
# File 'lib/magician/enumerable.rb', line 122

def occurences
  each_with_object(Hash.new 0) { |item, occurences| occurences[item] += 1 }
end

#palindrome?Boolean

Returns true if the Enumerable is a palindrome (meaning it is the same forward and backward). This method relies on a working each_with_index method on the Enumerable. However, Enumerable objects that are instances of String or one of its subclasses will rely on the each_char method instead.

Returns:

  • (Boolean)

    true if the Enumerable is a palindrome


132
133
134
135
136
137
138
# File 'lib/magician/enumerable.rb', line 132

def palindrome?
  enumerator = is_a?(String) ? each_char : each

  array_of_enum = enumerator.to_a

  array_of_enum == array_of_enum.reverse
end

#rangeNumeric?

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

Returns:

  • (Numeric, nil)

    the range of the elements of the Enumerable

Raises:

  • (RuntimeError)

    if the Enumerable contains non-Numeric objects


65
66
67
68
69
# File 'lib/magician/enumerable.rb', line 65

def range
  require_numerics

  empty? ? nil : max - min
end

#sumNumeric

Gets the sum of the Enumerable's elements. The sum of an empty Enumerable is

  1. The Enumerable must only contain Numerics or a RuntimeError will be

raised.

Returns:

  • (Numeric)

    the sum of the elements of the Enumerable

Raises:

  • (RuntimeError)

    if the Enumerable contains non-Numeric objects


20
21
22
23
24
# File 'lib/magician/enumerable.rb', line 20

def sum
  require_numerics

  empty? ? 0 : reduce(:+)
end