Module | Enumerable |
In: |
lib/backports/1.8.7/enumerable.rb
lib/backports/1.8.7/enumerator.rb lib/backports/1.9.1/enumerable.rb lib/backports/1.9.2/enumerable.rb lib/backports/rails/enumerable.rb |
MOST_EXTREME_OBJECT_EVER | = | Object.new # :nodoc: |
reduce | -> | self |
collect_concat | -> | self |
Standard in Ruby 1.9.2 See official documentation
# File lib/backports/1.9.2/enumerable.rb, line 3 3: def chunk(initial_state = nil, &original_block) 4: raise ArgumentError, "no block given" unless block_given? 5: ::Enumerator.new do |yielder| 6: previous = nil 7: accumulate = [] 8: block = initial_state.nil? ? original_block : Proc.new{|val| original_block.yield(val, initial_state.clone)} 9: each do |val| 10: key = block.yield(val) 11: if key.nil? || (key.is_a?(Symbol) && key.to_s[0,1] == "_") 12: yielder.yield [previous, accumulate] unless accumulate.empty? 13: accumulate = [] 14: previous = nil 15: case key 16: when nil, :_separator 17: when :_singleton 18: yielder.yield [key, [val]] 19: else 20: raise RuntimeError, "symbol beginning with an underscore are reserved" 21: end 22: else 23: if previous.nil? || previous == key 24: accumulate << val 25: else 26: yielder.yield [previous, accumulate] unless accumulate.empty? 27: accumulate = [val] 28: end 29: previous = key 30: end 31: end 32: # what to do in case of a break? 33: yielder.yield [previous, accumulate] unless accumulate.empty? 34: end 35: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/enumerable.rb, line 4 4: def count(item = Backports::Undefined) 5: seq = 0 6: if item != Backports::Undefined 7: each { |o| seq += 1 if item == o } 8: elsif block_given? 9: each { |o| seq += 1 if yield(o) } 10: else 11: each { seq += 1 } 12: end 13: seq 14: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/enumerable.rb, line 17 17: def cycle(n = nil) 18: return to_enum(:cycle, n) unless block_given? 19: if n.nil? 20: each{|e| yield e } until false 21: else 22: n = Backports.coerce_to_int(n) 23: if n >= 1 24: cache = [] 25: each do |elem| 26: cache << elem 27: yield elem 28: end 29: (n-1).times do 30: cache.each{|e| yield e } 31: end 32: end 33: end 34: nil 35: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/enumerable.rb, line 43 43: def drop(n) 44: n = Backports.coerce_to_int(n) 45: raise ArgumentError, "attempt to drop negative size" if n < 0 46: ary = to_a 47: return [] if n > ary.size 48: ary[n...ary.size] 49: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/enumerable.rb, line 52 52: def drop_while 53: return to_enum(:drop_while) unless block_given? 54: ary = [] 55: dropping = true 56: each do |obj| 57: ary << obj unless dropping &&= yield(obj) 58: end 59: ary 60: end
# File lib/backports/1.9.2/enumerable.rb, line 37 37: def each_entry(*pass) 38: return to_enum(:each_entry, *pass) unless block_given? 39: each(*pass) do |*args| 40: yield args.size == 1 ? args[0] : args 41: end 42: self 43: end
# File lib/backports/1.8.7/enumerable.rb, line 64 64: def each_with_index_with_optional_args_and_block(*args) 65: return to_enum(:each_with_index, *args) unless block_given? 66: idx = 0 67: each(*args) { |o| yield(o, idx); idx += 1 } 68: self 69: end
Standard in Ruby 1.8.8. See official documentation
# File lib/backports/1.9.1/enumerable.rb, line 3 3: def each_with_object(memo) 4: return to_enum(:each_with_object, memo) unless block_given? 5: each {|obj| yield obj, memo} 6: memo 7: end
# File lib/backports/1.8.7/enumerable.rb, line 247 247: def entries_with_optional_arguments(*args) 248: return entries_without_optional_arguments if args.empty? 249: to_enum(:each, *args).entries 250: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/enumerable.rb, line 74 74: def find_index(obj = Backports::Undefined) 75: if obj != Backports::Undefined 76: each_with_index do |element, i| 77: return i if element == obj 78: end 79: elsif block_given? 80: each_with_index do |element, i| 81: return i if yield element 82: end 83: else 84: return to_enum(:find_index) 85: end 86: nil 87: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/enumerable.rb, line 90 90: def first(n = Backports::Undefined) 91: return take(n) unless n == Backports::Undefined 92: each{|obj| return obj} 93: nil 94: end
# File lib/backports/1.9.2/enumerable.rb, line 45 45: def flat_map(&block) 46: return to_enum(:flat_map) unless block_given? 47: map(&block).flatten(1) 48: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/enumerable.rb, line 97 97: def group_by 98: return to_enum(:group_by) unless block_given? 99: {}.tap do |result| 100: each do |o| 101: result.fetch(yield(o)){|key| result[key] = []} << o 102: end 103: end 104: end
# File lib/backports/1.8.7/enumerable.rb, line 108 108: def inject_with_symbol(*args, &block) 109: return inject_without_symbol(*args, &block) if block_given? && args.size <= 1 110: method = args.pop 111: inject_without_symbol(*args) {|memo, obj| memo.send(method, obj)} 112: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/enumerable.rb, line 128 128: def max_by 129: return to_enum(:max_by) unless block_given? 130: max_object, max_result = nil, MOST_EXTREME_OBJECT_EVER 131: each do |object| 132: result = yield object 133: max_object, max_result = object, result if max_result < result 134: end 135: max_object 136: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/enumerable.rb, line 139 139: def min_by 140: return to_enum(:min_by) unless block_given? 141: min_object, min_result = nil, MOST_EXTREME_OBJECT_EVER 142: each do |object| 143: result = yield object 144: min_object, min_result = object, result if min_result > result 145: end 146: min_object 147: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/enumerable.rb, line 150 150: def minmax 151: return minmax{|a,b| a <=> b} unless block_given? 152: first_time = true 153: min, max = nil 154: each do |object| 155: if first_time 156: min = max = object 157: first_time = false 158: else 159: min = object if Backports.coerce_to_comparison(min, object, yield(min, object)) > 0 160: max = object if Backports.coerce_to_comparison(max, object, yield(max, object)) < 0 161: end 162: end 163: [min, max] 164: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/enumerable.rb, line 167 167: def minmax_by 168: return to_enum(:minmax_by) unless block_given? 169: min_object, min_result = nil, MOST_EXTREME_OBJECT_EVER 170: max_object, max_result = nil, MOST_EXTREME_OBJECT_EVER 171: each do |object| 172: result = yield object 173: min_object, min_result = object, result if min_result > result 174: max_object, max_result = object, result if max_result < result 175: end 176: [min_object, max_object] 177: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/enumerable.rb, line 180 180: def none?(&block) 181: !any?(&block) 182: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/enumerable.rb, line 185 185: def one? 186: found_one = false 187: if block_given? 188: each do |o| 189: if yield(o) 190: return false if found_one 191: found_one = true 192: end 193: end 194: else 195: each do |o| 196: if o 197: return false if found_one 198: found_one = true 199: end 200: end 201: end 202: found_one 203: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/enumerable.rb, line 208 208: def reverse_each 209: return to_enum(:reverse_each) unless block_given? 210: # There is no other way then to convert to an array first... see 1.9's source. 211: to_a.reverse_each{|e| yield e} 212: self 213: end
# File lib/backports/1.9.2/enumerable.rb, line 55 55: def slice_before(arg = Backports::Undefined, &block) 56: if block_given? 57: has_init = !(arg.equal? Backports::Undefined) 58: else 59: raise ArgumentError, "wrong number of arguments (0 for 1)" if arg.equal? Backports::Undefined 60: block = Proc.new{|elem| arg === elem } 61: end 62: Enumerator.new do |yielder| 63: init = arg.dup if has_init 64: accumulator = nil 65: each do |elem| 66: start_new = has_init ? block.yield(elem, init) : block.yield(elem) 67: if start_new 68: yielder.yield accumulator if accumulator 69: accumulator = [elem] 70: else 71: accumulator ||= [] 72: accumulator << elem 73: end 74: end 75: yielder.yield accumulator if accumulator 76: end 77: end
Standard in rails… See official documentation Modified from rails 2.3 to not rely on size
# File lib/backports/rails/enumerable.rb, line 4 4: def sum(identity = 0, &block) 5: if block_given? 6: map(&block).sum(identity) 7: else 8: inject { |sum, element| sum + element } || identity 9: end 10: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/enumerable.rb, line 216 216: def take(n) 217: n = Backports.coerce_to_int(n) 218: raise ArgumentError, "attempt to take negative size: #{n}" if n < 0 219: [].tap do |array| 220: each do |elem| 221: array << elem 222: break if array.size >= n 223: end unless n <= 0 224: end 225: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/enumerable.rb, line 228 228: def take_while 229: return to_enum(:take_while) unless block_given? 230: inject([]) do |array, elem| 231: return array unless yield elem 232: array << elem 233: end 234: end