Module: Enumerable
- Defined in:
- lib/nobiru/extensions/enumerable_extension.rb
Instance Method Summary collapse
- #drop_last(n) ⇒ Object
- #drop_last_while ⇒ Object
- #exactly?(n) ⇒ Boolean
- #frequencies ⇒ Object
- #max(identity = 0) ⇒ Object
- #mean(identity = 0) ⇒ Object
- #median(identity = 0) ⇒ Object
- #min(identity = 0) ⇒ Object
- #mode(identity = 0) ⇒ Object
- #range(identity = 0) ⇒ Object
- #several? ⇒ Boolean
- #standard_deviation ⇒ Object
- #sum(identity = 0) ⇒ Object
- #take_last(n) ⇒ Object
- #take_last_while ⇒ Object
- #variance ⇒ Object
Instance Method Details
#drop_last(n) ⇒ Object
9 10 11 12 13 14 |
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 9 def drop_last(n) array = to_a return array if n > array.size array[0...(array.size - n)] end |
#drop_last_while ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 16 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
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 27 def exactly?(n) found_count = 0 if block_given? each do |*o| found_count += 1 if yield(*o) end else each do |o| found_count += 1 if o end end (found_count > n) ? false : n == found_count end |
#frequencies ⇒ Object
43 44 45 |
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 43 def frequencies each_with_object(Hash.new(0)) { |e, a| a[e] += 1 } end |
#max(identity = 0) ⇒ Object
47 48 49 |
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 47 def max(identity=0) size > 0 ? sort.last : identity end |
#mean(identity = 0) ⇒ Object
55 56 57 58 |
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 55 def mean(identity=0) collection_size = size collection_size > 0 ? inject(:+) / collection_size.to_f : identity end |
#median(identity = 0) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 60 def median(identity=0) collection_size = size collection_sorted = sort if collection_size > 0 if collection_size % 2 == 0 (collection_sorted[(collection_size / 2) -1] + collection_sorted[collection_size / 2]) / 2.0 else collection_sorted[collection_size / 2] end else identity end end |
#min(identity = 0) ⇒ Object
51 52 53 |
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 51 def min(identity=0) size > 0 ? sort.first : identity end |
#mode(identity = 0) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 75 def mode(identity=0) if size > 0 frequency_distribution = inject(Hash.new(0)) { |h, v| h[v] += 1; h } frequency_top_2 = frequency_distribution.sort { |a, b| b[1] <=> a[1] }.take(2) if frequency_top_2.length == 1 frequency_top_2.first.first elsif frequency_top_2.first.last == frequency_top_2.last.last nil else frequency_top_2.first.first end else identity end end |
#range(identity = 0) ⇒ Object
92 93 94 95 |
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 92 def range(identity=0) collection_sorted = sort size > 0 ? collection_sorted.last - collection_sorted.first : identity end |
#several? ⇒ Boolean
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 97 def several? found_count = 0 if block_given? each do |*o| found_count += 1 if yield(*o) end else each do |o| found_count += 1 if o end end (found_count > 1) ? true : false end |
#standard_deviation ⇒ Object
113 114 115 116 |
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 113 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
118 119 120 121 122 123 |
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 118 def take_last(n) array = to_a return array if n > array.size array[(array.size - n)..-1] end |
#take_last_while ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 125 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 |
#variance ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/nobiru/extensions/enumerable_extension.rb', line 133 def variance collection_length = length return 0 if collection_length <= 1 sum = inject(0) { |accumulator, value| accumulator + (value - mean) ** 2 } sum / (collection_length.to_f - 1) end |