Module: Enumerable
- Defined in:
- lib/active_object/enumerable.rb
Instance Method Summary collapse
- #cluster(&block) ⇒ Object
- #difference(identity = 0, &block) ⇒ Object
- #divisible(identity = 0, &block) ⇒ Object
- #drop_last(n) ⇒ Object
- #drop_last_if ⇒ Object
- #exactly?(n) ⇒ Boolean
- #exclude?(object) ⇒ Boolean
- #expand ⇒ Object
- #exponential(identity = 0, &block) ⇒ Object
- #frequencies ⇒ Object
- #incase?(object) ⇒ Boolean
- #many? ⇒ Boolean
- #max(identity = 0) ⇒ Object
- #mean(identity = 0) ⇒ Object (also: #average)
- #median(identity = 0) ⇒ Object
- #min(identity = 0) ⇒ Object
- #mode(identity = 0) ⇒ Object
- #multiple(identity = 0, &block) ⇒ Object
- #range(identity = 0) ⇒ Object
- #several? ⇒ Boolean
- #standard_deviation(identity = 0) ⇒ Object
- #sum(identity = 0, &block) ⇒ Object
- #take_last(n) ⇒ Object
- #take_last_if ⇒ Object
- #variance(identity = 0) ⇒ Object
Instance Method Details
#cluster(&block) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/active_object/enumerable.rb', line 3 def cluster(&block) result = [] each do |element| if result.last && (yield(result.last.last) == yield(element)) result.last << element else result << [element] end end result end |
#difference(identity = 0, &block) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/active_object/enumerable.rb', line 15 def difference(identity=0, &block) if block_given? map(&block).difference(identity) else inject { |d, v| d - v } || identity end end |
#divisible(identity = 0, &block) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/active_object/enumerable.rb', line 23 def divisible(identity=0, &block) if block_given? map(&block).divisible(identity) else inject { |d, v| d / v } || identity end end |
#drop_last(n) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/active_object/enumerable.rb', line 31 def drop_last(n) collection_length = to_a.length return(self) if n > collection_length self[0...(collection_length - n)] end |
#drop_last_if ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/active_object/enumerable.rb', line 39 def drop_last_if return(to_enum(:drop_last_if)) unless block_given? result = [] dropping = true reverse_each do |value| result.unshift(value) unless dropping &&= yield(value) end result end |
#exactly?(n) ⇒ Boolean
50 51 52 53 54 |
# File 'lib/active_object/enumerable.rb', line 50 def exactly?(n) found_count = 0 block_given? ? each { |*o| found_count += 1 if yield(*o) } : each { |o| found_count += 1 if o } (found_count > n) ? false : n == found_count end |
#exclude?(object) ⇒ Boolean
56 57 58 |
# File 'lib/active_object/enumerable.rb', line 56 def exclude?(object) !include?(object) end |
#expand ⇒ Object
60 61 62 |
# File 'lib/active_object/enumerable.rb', line 60 def map { |n| n.is_a?(Enumerable) ? n. : n } end |
#exponential(identity = 0, &block) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/active_object/enumerable.rb', line 64 def exponential(identity=0, &block) if block_given? map(&block).exponential(identity) else inject { |d, v| d ** v } || identity end end |
#frequencies ⇒ Object
72 73 74 |
# File 'lib/active_object/enumerable.rb', line 72 def frequencies each_with_object(Hash.new(0)) { |e, a| a[e] += 1 } end |
#incase?(object) ⇒ Boolean
76 77 78 |
# File 'lib/active_object/enumerable.rb', line 76 def incase?(object) any? { |x| object === x } end |
#many? ⇒ Boolean
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/active_object/enumerable.rb', line 80 def many? found_count = 0 if block_given? any? do |v| found_count += 1 if yield v found_count > 1 end else any? { (found_count += 1) > 1 } end end |
#max(identity = 0) ⇒ Object
92 93 94 |
# File 'lib/active_object/enumerable.rb', line 92 def max(identity=0) (length rescue count) > 0 ? sort.last : identity end |
#mean(identity = 0) ⇒ Object Also known as: average
100 101 102 103 104 105 |
# File 'lib/active_object/enumerable.rb', line 100 def mean(identity=0) return(identity) unless length > 0 collection_length = length sum.to_f / collection_length.to_f end |
#median(identity = 0) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/active_object/enumerable.rb', line 109 def median(identity=0) collection_length = length.to_f collection_sorted = sort return(identity) unless collection_length > 0 if (collection_length % 2).zero? (collection_sorted[(collection_length / 2.0) -1.0] + collection_sorted[collection_length / 2.0]) / 2.0 else collection_sorted[collection_length / 2.0] end end |
#min(identity = 0) ⇒ Object
96 97 98 |
# File 'lib/active_object/enumerable.rb', line 96 def min(identity=0) (length rescue count) > 0 ? sort.first : identity end |
#mode(identity = 0) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/active_object/enumerable.rb', line 122 def mode(identity=0) return(identity) unless length > 0 frequency_distribution = inject(Hash.new(0)) { |h, v| h[v] += 1; h } frequency_top_two = frequency_distribution.sort { |k, v| v[1] <=> k[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 end |
#multiple(identity = 0, &block) ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/active_object/enumerable.rb', line 137 def multiple(identity=0, &block) if block_given? map(&block).multiple(identity) else inject { |d, v| d * v } || identity end end |
#range(identity = 0) ⇒ Object
145 146 147 148 149 150 |
# File 'lib/active_object/enumerable.rb', line 145 def range(identity=0) return(identity) unless length > 0 collection_sorted = sort collection_sorted.last - collection_sorted.first end |
#several? ⇒ Boolean
152 153 154 155 156 |
# File 'lib/active_object/enumerable.rb', line 152 def several? found_count = 0 block_given? ? each { |*o| found_count += 1 if yield(*o) } : each { |o| found_count += 1 if o } (found_count > 1) ? true : false end |
#standard_deviation(identity = 0) ⇒ Object
158 159 160 161 162 |
# File 'lib/active_object/enumerable.rb', line 158 def standard_deviation(identity=0) return(identity) if length < 2 Math.sqrt(variance) end |
#sum(identity = 0, &block) ⇒ Object
164 165 166 167 168 169 170 |
# File 'lib/active_object/enumerable.rb', line 164 def sum(identity=0, &block) if block_given? map(&block).sum(identity) else inject { |s, v| s + v } || identity end end |
#take_last(n) ⇒ Object
172 173 174 175 176 177 178 |
# File 'lib/active_object/enumerable.rb', line 172 def take_last(n) collection_length = to_a.length return(self) if n > collection_length self[(collection_length - n)..-1] end |
#take_last_if ⇒ Object
180 181 182 183 184 185 186 |
# File 'lib/active_object/enumerable.rb', line 180 def take_last_if return(to_enum(:take_last_if)) unless block_given? result = [] reverse_each { |e| yield(e) ? result.unshift(e) : break } result end |
#variance(identity = 0) ⇒ Object
188 189 190 191 192 193 194 195 |
# File 'lib/active_object/enumerable.rb', line 188 def variance(identity=0) collection_length = length return(identity) if collection_length <= 1 sum = inject(0.0) { |s,v| s + (v - mean) ** 2.0 } sum.to_f / (collection_length.to_f - 1.0) end |