Class: Array
- Defined in:
- lib/backports/1.9/array.rb,
lib/backports/1.8.7/array.rb,
lib/backports/rails/array.rb
Class Method Summary collapse
-
.try_convert(obj) ⇒ Object
Try to convert obj into an array, using to_ary method.
Instance Method Summary collapse
-
#combination(num) ⇒ Object
Standard in Ruby 1.8.7+.
-
#cycle(n = nil, &block) ⇒ Object
Standard in Ruby 1.8.7+.
-
#extract_options! ⇒ Object
See official documentation.
-
#flatten_with_optional_argument(level = -1)) ⇒ Object
Recursively flatten any contained Arrays into an one-dimensional result.
-
#flatten_with_optional_argument!(level = -1)) ⇒ Object
Flattens self in place as #flatten.
- #index_with_block(*arg) ⇒ Object
- #pop_with_optional_argument(n = Backports::Undefined) ⇒ Object
-
#product(*arg) ⇒ Object
Standard in Ruby 1.8.7+.
- #rindex_with_block(*arg) ⇒ Object
-
#sample(n = Backports::Undefined) ⇒ Object
Standard in Ruby 1.8.7+.
- #shift_with_optional_argument(n = Backports::Undefined) ⇒ Object
-
#shuffle ⇒ Object
Standard in Ruby 1.8.7+.
-
#shuffle! ⇒ Object
Standard in Ruby 1.8.7+.
Class Method Details
.try_convert(obj) ⇒ Object
Try to convert obj into an array, using to_ary method. Returns converted array or nil if obj cannot be converted for any reason. This method is to check if an argument is an array.
7 8 9 10 |
# File 'lib/backports/1.9/array.rb', line 7 def try_convert(obj) return nil unless obj.respond_to?(:to_ary) Backports.coerce_to(obj, Array, :to_ary) end |
Instance Method Details
#combination(num) ⇒ Object
Standard in Ruby 1.8.7+. See official documentation
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/backports/1.8.7/array.rb', line 3 def combination(num) num = Backports.coerce_to num, Fixnum, :to_int return to_enum(:combination, num) unless block_given? return self unless (0..size).include? num # Implementation note: slightly tricky. # Example: self = 1..7, num = 3 picks = (0...num).to_a # picks start at 0, 1, 2 max = ((size-num)...size).to_a # max (index for a given pick) is [4, 5, 6] pick_max_pairs = picks.zip(max).reverse # pick_max_pairs = [[2, 6], [1, 5], [0, 4]] lookup = pick_max_pairs.find(Proc.new{return self}) loop do yield values_at(*picks) move = lookup.each{|pick, max| picks[pick] < max}.first new_index = picks[move] + 1 picks[move...num] = (new_index...(new_index+num-move)).to_a end end |
#cycle(n = nil, &block) ⇒ Object
Standard in Ruby 1.8.7+. See official documentation
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/backports/1.8.7/array.rb', line 22 def cycle(n = nil, &block) return to_enum(:cycle, n) unless block_given? if n.nil? loop(&block) else n = Backports.coerce_to n, Fixnum, :to_int n.times{each(&block)} end nil end |
#extract_options! ⇒ Object
See official documentation
3 4 5 |
# File 'lib/backports/rails/array.rb', line 3 def last.is_a?(::Hash) ? pop : {} end |
#flatten_with_optional_argument(level = -1)) ⇒ Object
Recursively flatten any contained Arrays into an one-dimensional result. Adapted from rubinius’
41 42 43 |
# File 'lib/backports/1.8.7/array.rb', line 41 def flatten_with_optional_argument(level=-1) dup.flatten!(level) || self end |
#flatten_with_optional_argument!(level = -1)) ⇒ Object
Flattens self in place as #flatten. If no changes are made, returns nil, otherwise self. Adapted from rubinius’
48 49 50 51 52 53 54 55 56 |
# File 'lib/backports/1.8.7/array.rb', line 48 def flatten_with_optional_argument!(level=-1) level = Backports.coerce_to(level, Integer, :to_int) return flatten_without_optional_argument! unless level >= 0 ret, out = nil, [] ret = recursively_flatten_finite(self, out, level) replace(out) if ret ret end |
#index_with_block(*arg) ⇒ Object
84 85 86 87 88 |
# File 'lib/backports/1.8.7/array.rb', line 84 def index_with_block(*arg) return index_without_block(*arg) unless block_given? && arg.empty? each_with_index{|o,i| return i if yield o} return nil end |
#pop_with_optional_argument(n = Backports::Undefined) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/backports/1.8.7/array.rb', line 95 def pop_with_optional_argument(n = Backports::Undefined) return pop_without_optional_argument if n == Backports::Undefined n = Backports.coerce_to(n, Fixnum, :to_int) raise ArgumentError, "negative array size" if n < 0 first = size - n first = 0 if first < 0 slice!(first..size).to_a end |
#product(*arg) ⇒ Object
Standard in Ruby 1.8.7+. See official documentation
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/backports/1.8.7/array.rb', line 107 def product(*arg) # Implementation notes: We build an enumerator for all the combinations # by building it up successively using "inject" and starting from a trivial enumerator. # It would be easy to have "product" yield to a block but the standard # simply returns an array, so you'll find a simple call to "to_a" at the end. # trivial_enum = Enumerator.new(Backports::Yielder.new{|yielder| yielder.yield [] }) # Enumerator.new{...} is 1.9+ only [self, *arg].map{|x| Backports.coerce_to(x, Array, :to_ary)}. inject(trivial_enum) do |enum, array| Enumerator.new(Backports::Yielder.new do |yielder| # Enumerator.new{...} is 1.9+ only enum.each do |partial_product| array.each do |obj| yielder.yield partial_product + [obj] end end end) end.to_a end |
#rindex_with_block(*arg) ⇒ Object
128 129 130 131 132 |
# File 'lib/backports/1.8.7/array.rb', line 128 def rindex_with_block(*arg) return rindex_without_block(*arg) unless block_given? && arg.empty? reverse_each.each_with_index{|o,i| return size - 1 - i if yield o} return nil end |
#sample(n = Backports::Undefined) ⇒ Object
Standard in Ruby 1.8.7+. See official documentation
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/backports/1.8.7/array.rb', line 137 def sample(n = Backports::Undefined) return self[rand(size)] if n == Backports::Undefined n = Backports.coerce_to(n, Fixnum, :to_int) raise ArgumentError, "negative array size" if n < 0 n = size if n > size result = Array.new(self) n.times do |i| r = i + rand(size - i) result[i], result[r] = result[r], result[i] end result[n..size] = [] result end |
#shift_with_optional_argument(n = Backports::Undefined) ⇒ Object
153 154 155 156 157 158 |
# File 'lib/backports/1.8.7/array.rb', line 153 def shift_with_optional_argument(n = Backports::Undefined) return shift_without_optional_argument if n == Backports::Undefined n = Backports.coerce_to(n, Fixnum, :to_int) raise ArgumentError, "negative array size" if n < 0 slice!(0, n) end |
#shuffle ⇒ Object
Standard in Ruby 1.8.7+. See official documentation
163 164 165 |
# File 'lib/backports/1.8.7/array.rb', line 163 def shuffle dup.shuffle! end |
#shuffle! ⇒ Object
Standard in Ruby 1.8.7+. See official documentation
168 169 170 171 172 173 174 |
# File 'lib/backports/1.8.7/array.rb', line 168 def shuffle! size.times do |i| r = i + rand(size - i) self[i], self[r] = self[r], self[i] end self end |