Module: Enumerable
- Defined in:
- lib/active_object/enumerable.rb
Instance Method Summary collapse
- #difference(identity = 0, &block) ⇒ Object
- #divisible(identity = 0, &block) ⇒ Object
- #drop_last(n) ⇒ Object
- #drop_last_if ⇒ Object
- #exactly?(n) ⇒ Boolean
- #exclude?(object) ⇒ Boolean
- #exponential(identity = 0, &block) ⇒ Object
- #frequencies ⇒ Object
- #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
#difference(identity = 0, &block) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/active_object/enumerable.rb', line 3 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
11 12 13 14 15 16 17 |
# File 'lib/active_object/enumerable.rb', line 11 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
19 20 21 22 23 |
# File 'lib/active_object/enumerable.rb', line 19 def drop_last(n) collection_size = to_a.size return(self) if n > collection_size self[0...(collection_size - n)] end |
#drop_last_if ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/active_object/enumerable.rb', line 25 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
36 37 38 39 40 |
# File 'lib/active_object/enumerable.rb', line 36 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
43 44 45 |
# File 'lib/active_object/enumerable.rb', line 43 def exclude?(object) !include?(object) end |
#exponential(identity = 0, &block) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/active_object/enumerable.rb', line 48 def exponential(identity=0, &block) if block_given? map(&block).exponential(identity) else inject { |d,v| d ** v } || identity end end |
#frequencies ⇒ Object
56 57 58 |
# File 'lib/active_object/enumerable.rb', line 56 def frequencies each_with_object(Hash.new(0)) { |e, a| a[e] += 1 } end |
#many? ⇒ Boolean
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/active_object/enumerable.rb', line 61 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
74 75 76 |
# File 'lib/active_object/enumerable.rb', line 74 def max(identity=0) size > 0 ? sort.last : identity end |
#mean(identity = 0) ⇒ Object
82 83 84 85 86 |
# File 'lib/active_object/enumerable.rb', line 82 def mean(identity=0) return(identity) unless size > 0 collection_size = size sum / collection_size.to_f end |
#median(identity = 0) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/active_object/enumerable.rb', line 88 def median(identity=0) collection_size = size collection_sorted = sort return(identity) unless 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 end |
#min(identity = 0) ⇒ Object
78 79 80 |
# File 'lib/active_object/enumerable.rb', line 78 def min(identity=0) size > 0 ? sort.first : identity end |
#mode(identity = 0) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/active_object/enumerable.rb', line 101 def mode(identity=0) return(identity) unless size > 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.size == 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
116 117 118 119 120 121 122 |
# File 'lib/active_object/enumerable.rb', line 116 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
124 125 126 127 128 |
# File 'lib/active_object/enumerable.rb', line 124 def range(identity=0) return(identity) unless size > 0 collection_sorted = sort collection_sorted.last - collection_sorted.first end |
#several? ⇒ Boolean
130 131 132 133 134 |
# File 'lib/active_object/enumerable.rb', line 130 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
136 137 138 139 |
# File 'lib/active_object/enumerable.rb', line 136 def standard_deviation(identity=0) return(identity) if size < 2 Math.sqrt(variance) end |
#sum(identity = 0, &block) ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/active_object/enumerable.rb', line 142 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
151 152 153 154 155 |
# File 'lib/active_object/enumerable.rb', line 151 def take_last(n) collection_size = to_a.size return(self) if n > collection_size self[(collection_size - n)..-1] end |
#take_last_if ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/active_object/enumerable.rb', line 157 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
165 166 167 168 169 170 |
# File 'lib/active_object/enumerable.rb', line 165 def variance(identity=0) collection_size = size return(identity) if collection_size <= 1 sum = inject(0.0) { |s,v| s + (v - mean) ** 2.0 } sum / (collection_size.to_f - 1.0) end |