Module: Enumerable

Defined in:
lib/nobiru/extensions/enumerable_extension.rb

Instance Method Summary collapse

Instance Method Details

#difference(identity = 0) ⇒ Object



9
10
11
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 9

def difference(identity=0)
  inject(:-) || identity
end

#divisible(identity = 0) ⇒ Object



13
14
15
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 13

def divisible(identity=0)
  inject(:/) || identity
end

#drop_last(n) ⇒ Object



17
18
19
20
21
22
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 17

def drop_last(n)
  array = to_a

  return(array) if n > array.size
  array[0...(array.size - n)]
end

#drop_last_whileObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 24

def drop_last_while
  return(to_enum(:drop_last_while)) unless block_given?

  result   = []
  dropping = true
  reverse_each do |value|
    result.unshift(value) unless dropping &&= yield(value)
  end
  result
end

#exactly?(n) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 35

def exactly?(n)
  found_count = 0

  if block_given?
    each { |*o| found_count += 1 if yield(*o) }
  else
    each { |o| found_count += 1 if o }
  end

  (found_count > n) ? false : n == found_count
end

#exponential(identity = 0) ⇒ Object



47
48
49
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 47

def exponential(identity=0)
  inject(:**) || identity
end

#frequenciesObject



51
52
53
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 51

def frequencies
  each_with_object(Hash.new(0)) { |e, a| a[e] += 1 }
end

#max(identity = 0) ⇒ Object



55
56
57
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 55

def max(identity=0)
  size > 0 ? sort.last : identity
end

#mean(identity = 0) ⇒ Object



63
64
65
66
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 63

def mean(identity=0)
  collection_size = size
  collection_size > 0 ? inject(:+) / collection_size.to_f : identity
end

#median(identity = 0) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 68

def median(identity=0)
  collection_size   = size
  collection_sorted = sort

  if collection_size > 0
    if (collection_size % 2).zero?
      (collection_sorted[(collection_size / 2.0) -1.0] + collection_sorted[collection_size / 2.0]) / 2.0
    else
      collection_sorted[collection_size / 2.0]
    end
  else
    identity
  end
end

#min(identity = 0) ⇒ Object



59
60
61
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 59

def min(identity=0)
  size > 0 ? sort.first : identity
end

#mode(identity = 0) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 83

def mode(identity=0)
  if size > 0
    frequency_distribution = inject(Hash.new(0)) { |h, v| h[v] += 1; h }
    frequency_top_two      = frequency_distribution.sort { |a, b| b[1] <=> a[1] }.take(2)

    if frequency_top_two.length == 1
      frequency_top_two.first.first
    elsif frequency_top_two.first.last == frequency_top_two.last.last
      nil
    else
      frequency_top_two.first.first
    end
  else
    identity
  end
end

#multiple(identity = 0) ⇒ Object



100
101
102
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 100

def multiple(identity=0)
  inject(:*) || identity
end

#range(identity = 0) ⇒ Object



104
105
106
107
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 104

def range(identity=0)
  collection_sorted = sort
  size > 0 ? collection_sorted.last - collection_sorted.first : identity
end

#several?Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
115
116
117
118
119
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 109

def several?
  found_count = 0

  if block_given?
    each { |*o| found_count += 1 if yield(*o) }
  else
    each { |o| found_count += 1 if o }
  end

  (found_count > 1) ? true : false
end

#standard_deviationObject



121
122
123
124
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 121

def standard_deviation
  return(0) if length < 2
  Math.sqrt(variance)
end

#sum(identity = 0) ⇒ Object



4
5
6
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 4

def sum(identity=0)
  inject(:+) || identity
end

#take_last(n) ⇒ Object



126
127
128
129
130
131
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 126

def take_last(n)
  array = to_a

  return(array) if n > array.size
  array[(array.size - n)..-1]
end

#take_last_whileObject



133
134
135
136
137
138
139
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 133

def take_last_while
  return(to_enum(:take_last_while)) unless block_given?

  result = []
  reverse_each { |e| yield(e) ? result.unshift(e) : break }
  result
end

#varianceObject



141
142
143
144
145
146
147
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 141

def variance
  collection_length = length

  return(0) if collection_length <= 1
  sum = inject(0.0) { |accumulator, value| accumulator + (value - mean) ** 2.0 }
  sum / (collection_length.to_f - 1.0)
end