Class | Array |
In: |
lib/backports/1.8.7/array.rb
lib/backports/1.9.1/array.rb lib/backports/1.9.2/array.rb lib/backports/rails/array.rb |
Parent: | Object |
# File lib/backports/1.9.1/array.rb, line 4 4: def try_convert(obj) 5: Backports.try_convert(obj, Array, :to_ary) 6: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/array.rb, line 3 3: def combination(num) 4: num = Backports.coerce_to_int(num) 5: return to_enum(:combination, num) unless block_given? 6: return self unless (0..size).include? num 7: # Implementation note: slightly tricky. 8: # Example: self = 1..7, num = 3 9: picks = (0...num).to_a # picks start at 0, 1, 2 10: max_index = ((size-num)...size).to_a # max (index for a given pick) is [4, 5, 6] 11: pick_max_pairs = picks.zip(max_index).reverse # pick_max_pairs = [[2, 6], [1, 5], [0, 4]] 12: lookup = pick_max_pairs.find(Proc.new{return self}) 13: loop do 14: yield values_at(*picks) 15: move = lookup.each{|pick, max| picks[pick] < max}.first 16: new_index = picks[move] + 1 17: picks[move...num] = (new_index...(new_index+num-move)).to_a 18: end 19: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/array.rb, line 22 22: def cycle(n = nil) 23: return to_enum(:cycle, n) unless block_given? 24: if n.nil? 25: each{|e| yield e } until false 26: else 27: n = Backports.coerce_to_int(n) 28: n.times{each{|e| yield e }} 29: end 30: nil 31: end
See official documentation
# File lib/backports/rails/array.rb, line 3 3: def extract_options! 4: last.is_a?(::Hash) ? pop : {} 5: end
Recursively flatten any contained Arrays into an one-dimensional result. Adapted from rubinius‘
# File lib/backports/1.8.7/array.rb, line 41 41: def flatten_with_optional_argument(level=-1) 42: dup.flatten!(level) || self 43: end
Flattens self in place as flatten. If no changes are made, returns nil, otherwise self. Adapted from rubinius‘
# File lib/backports/1.8.7/array.rb, line 48 48: def flatten_with_optional_argument!(level=-1) 49: level = Backports.coerce_to_int(level) 50: return flatten_without_optional_argument! unless level >= 0 51: 52: ret, out = nil, [] 53: ret = recursively_flatten_finite(self, out, level) 54: replace(out) if ret 55: ret 56: end
# File lib/backports/1.8.7/array.rb, line 84 84: def index_with_block(*arg) 85: return index_without_block(*arg) unless block_given? && arg.empty? 86: each_with_index{|o,i| return i if yield o} 87: return nil 88: end
Standard in Ruby 1.9.2 See official documentation
# File lib/backports/1.9.2/array.rb, line 3 3: def keep_if 4: return to_enum(:keep_if) unless block_given? 5: delete_if{|elem| !yield elem} 6: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/array.rb, line 94 94: def permutation(num = Backports::Undefined) 95: return to_enum(:permutation, num) unless block_given? 96: num = num.equal?(Backports::Undefined) ? 97: size : 98: Backports.coerce_to_int(num) 99: return self unless (0..size).include? num 100: 101: final_lambda = lambda do |partial, remain| 102: yield partial 103: end 104: 105: outer_lambda = num.times.inject(final_lambda) do |proc, ignore| 106: lambda do |partial, remain| 107: remain.each_with_index do |val, i| 108: new_remain = remain.dup 109: new_remain.delete_at(i) 110: proc.call(partial.dup << val, new_remain) 111: end 112: end 113: end 114: 115: outer_lambda.call([], self) 116: end
# File lib/backports/1.8.7/array.rb, line 120 120: def pop_with_optional_argument(n = Backports::Undefined) 121: return pop_without_optional_argument if n == Backports::Undefined 122: n = Backports.coerce_to_int(n) 123: raise ArgumentError, "negative array size" if n < 0 124: first = size - n 125: first = 0 if first < 0 126: slice!(first..size).to_a 127: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/array.rb, line 132 132: def product(*arg) 133: # Implementation notes: We build a block that will generate all the combinations 134: # by building it up successively using "inject" and starting with one 135: # responsible to append the values. 136: # 137: result = [] 138: 139: arg.map!{|ary| Backports.coerce_to_ary(ary)} 140: arg.reverse! # to get the results in the same order as in MRI, vary the last argument first 141: arg.push self 142: 143: outer_lambda = arg.inject(result.method(:push)) do |proc, values| 144: lambda do |partial| 145: values.each do |val| 146: proc.call(partial.dup << val) 147: end 148: end 149: end 150: 151: outer_lambda.call([]) 152: 153: result 154: end
# File lib/backports/1.9.2/array.rb, line 9 9: def product_with_block(*arg, &block) 10: return product_without_block(*arg) unless block_given? 11: # Same implementation as 1.8.7, but yielding 12: arg.map!{|ary| Backports.coerce_to_ary(ary)} 13: arg.reverse! # to get the results in the same order as in MRI, vary the last argument first 14: arg.push self 15: 16: outer_lambda = arg.inject(block) do |proc, values| 17: lambda do |partial| 18: values.each do |val| 19: proc.call(partial.dup << val) 20: end 21: end 22: end 23: 24: outer_lambda.call([]) 25: self 26: end
Helper to recurse through flattening Adapted from rubinius’; recursion guards are not needed because level is finite
# File lib/backports/1.8.7/array.rb, line 63 63: def recursively_flatten_finite(array, out, level) 64: ret = nil 65: if level <= 0 66: out.concat(array) 67: else 68: array.each do |o| 69: if o.respond_to? :to_ary 70: recursively_flatten_finite(o.to_ary, out, level - 1) 71: ret = self 72: else 73: out << o 74: end 75: end 76: end 77: ret 78: end
Note: Combinations are not yielded in the same order as MRI. This is not a bug; the spec states that the order is implementation dependent
# File lib/backports/1.9.2/array.rb, line 32 32: def repeated_combination(num) 33: return to_enum(:repeated_combination, num) unless block_given? 34: num = Backports.coerce_to_int(num) 35: if num <= 0 36: yield [] if num == 0 37: else 38: indices = Array.new(num, 0) 39: indices[-1] = size 40: while dec = indices.find_index(&:nonzero?) 41: indices[0..dec] = Array.new dec+1, indices[dec]-1 42: yield values_at(*indices) 43: end 44: end 45: self 46: end
Note: Permutations are not yielded in the same order as MRI. This is not a bug; the spec states that the order is implementation dependent
# File lib/backports/1.9.2/array.rb, line 50 50: def repeated_permutation(num) 51: return to_enum(:repeated_permutation, num) unless block_given? 52: num = Backports.coerce_to_int(num) 53: if num <= 0 54: yield [] if num == 0 55: else 56: indices = Array.new(num, 0) 57: indices[-1] = size 58: while dec = indices.find_index(&:nonzero?) 59: indices[0...dec] = Array.new dec, size-1 60: indices[dec] -= 1 61: yield values_at(*indices) 62: end 63: end 64: self 65: end
# File lib/backports/1.8.7/array.rb, line 158 158: def rindex_with_block(*arg) 159: return rindex_without_block(*arg) unless block_given? && arg.empty? 160: reverse_each.each_with_index{|o,i| return size - 1 - i if yield o} 161: return nil 162: end
# File lib/backports/1.9.2/array.rb, line 67 67: def rotate(n=1) 68: Array.new(self).rotate!(n) 69: end
# File lib/backports/1.9.2/array.rb, line 71 71: def rotate!(n=1) 72: n = Backports.coerce_to_int(n) % (empty? ? 1 : size) 73: concat(shift(n)) 74: end
Standard in Ruby 1.8.7+. See official documentation Note: was named choice in 1.8.7 and renamed in later versions
# File lib/backports/1.8.7/array.rb, line 168 168: def sample(n = Backports::Undefined) 169: return self[Kernel.rand(size)] if n == Backports::Undefined 170: n = Backports.coerce_to_int(n) 171: raise ArgumentError, "negative array size" if n < 0 172: n = size if n > size 173: result = Array.new(self) 174: n.times do |i| 175: r = i + Kernel.rand(size - i) 176: result[i], result[r] = result[r], result[i] 177: end 178: result[n..size] = [] 179: result 180: end
# File lib/backports/1.9.2/array.rb, line 76 76: def select! 77: return to_enum(:select!) unless block_given? 78: reject!{|elem| ! yield elem} 79: end
# File lib/backports/1.8.7/array.rb, line 184 184: def shift_with_optional_argument(n = Backports::Undefined) 185: return shift_without_optional_argument if n == Backports::Undefined 186: n = Backports.coerce_to_int(n) 187: raise ArgumentError, "negative array size" if n < 0 188: slice!(0, n) 189: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/array.rb, line 194 194: def shuffle 195: dup.shuffle! 196: end
Standard in Ruby 1.8.7+. See official documentation
# File lib/backports/1.8.7/array.rb, line 199 199: def shuffle! 200: raise TypeError, "can't modify frozen array" if frozen? 201: size.times do |i| 202: r = i + Kernel.rand(size - i) 203: self[i], self[r] = self[r], self[i] 204: end 205: self 206: end
# File lib/backports/1.9.2/array.rb, line 81 81: def sort_by! 82: return to_enum(:sort_by!) unless block_given? 83: raise "can't modify frozen array" if frozen? 84: replace sort_by{|e| yield e} 85: end
# File lib/backports/1.9.2/array.rb, line 98 98: def uniq_with_block 99: return uniq_without_block unless block_given? 100: h = {} 101: each do |elem| 102: h[yield(elem)] ||= elem 103: end 104: h.values 105: end