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
- #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 14 15 |
# 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
17 18 19 20 21 22 23 |
# File 'lib/active_object/enumerable.rb', line 17 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
25 26 27 28 29 30 31 |
# File 'lib/active_object/enumerable.rb', line 25 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
33 34 35 36 37 38 39 |
# File 'lib/active_object/enumerable.rb', line 33 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
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/active_object/enumerable.rb', line 41 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
52 53 54 55 56 |
# File 'lib/active_object/enumerable.rb', line 52 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
59 60 61 |
# File 'lib/active_object/enumerable.rb', line 59 def exclude?(object) !include?(object) end |
#expand ⇒ Object
64 65 66 |
# File 'lib/active_object/enumerable.rb', line 64 def map { |n| n.is_a?(Enumerable) ? n. : n } end |
#exponential(identity = 0, &block) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/active_object/enumerable.rb', line 68 def exponential(identity=0, &block) if block_given? map(&block).exponential(identity) else inject { |d, v| d ** v } || identity end end |
#frequencies ⇒ Object
76 77 78 |
# File 'lib/active_object/enumerable.rb', line 76 def frequencies each_with_object(Hash.new(0)) { |e, a| a[e] += 1 } end |
#incase?(object) ⇒ Boolean
80 81 82 |
# File 'lib/active_object/enumerable.rb', line 80 def incase?(object) any? { |x| object === x } end |
#many? ⇒ Boolean
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/active_object/enumerable.rb', line 85 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
98 99 100 |
# File 'lib/active_object/enumerable.rb', line 98 def max(identity=0) (length rescue count) > 0 ? sort.last : identity end |
#mean(identity = 0) ⇒ Object
106 107 108 109 110 111 |
# File 'lib/active_object/enumerable.rb', line 106 def mean(identity=0) return(identity) unless length > 0 collection_length = length sum.to_f / collection_length.to_f end |
#median(identity = 0) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/active_object/enumerable.rb', line 113 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
102 103 104 |
# File 'lib/active_object/enumerable.rb', line 102 def min(identity=0) (length rescue count) > 0 ? sort.first : identity end |
#mode(identity = 0) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/active_object/enumerable.rb', line 126 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
141 142 143 144 145 146 147 |
# File 'lib/active_object/enumerable.rb', line 141 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
149 150 151 152 153 154 |
# File 'lib/active_object/enumerable.rb', line 149 def range(identity=0) return(identity) unless length > 0 collection_sorted = sort collection_sorted.last - collection_sorted.first end |
#several? ⇒ Boolean
156 157 158 159 160 |
# File 'lib/active_object/enumerable.rb', line 156 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
162 163 164 165 166 |
# File 'lib/active_object/enumerable.rb', line 162 def standard_deviation(identity=0) return(identity) if length < 2 Math.sqrt(variance) end |
#sum(identity = 0, &block) ⇒ Object
169 170 171 172 173 174 175 |
# File 'lib/active_object/enumerable.rb', line 169 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
178 179 180 181 182 183 184 |
# File 'lib/active_object/enumerable.rb', line 178 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
186 187 188 189 190 191 192 |
# File 'lib/active_object/enumerable.rb', line 186 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
194 195 196 197 198 199 200 201 |
# File 'lib/active_object/enumerable.rb', line 194 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 |