4 # ary.uniq! -> ary or nil
6 # Removes duplicate elements from +self+.
7 # Returns <code>nil</code> if no changes are made (that is, no
8 # duplicates are found).
10 # a = [ "a", "a", "b", "b", "c" ]
11 # a.uniq! #=> ["a", "b", "c"]
12 # b = [ "a", "b", "c" ]
20 ary.delete(result.last)
22 if result.size == self.size
33 # Returns a new array by removing duplicate values in +self+.
35 # a = [ "a", "a", "b", "b", "c" ]
36 # a.uniq #=> ["a", "b", "c"]
46 # ary - other_ary -> new_ary
48 # Array Difference---Returns a new array that is a copy of
49 # the original array, removing any items that also appear in
50 # <i>other_ary</i>. (If you need set-like behavior, see the
53 # [ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ] #=> [ 3, 3, 5 ]
56 raise TypeError, "can't convert #{elem.class} into Array" unless elem.class == Array
60 elem.each { |x| hash[x] = true }
61 self.each { |x| array << x unless hash[x] }
67 # ary | other_ary -> new_ary
69 # Set Union---Returns a new array by joining this array with
70 # <i>other_ary</i>, removing duplicates.
72 # [ "a", "b", "c" ] | [ "c", "d", "a" ]
73 # #=> [ "a", "b", "c", "d" ]
76 raise TypeError, "can't convert #{elem.class} into Array" unless elem.class == Array
84 # ary & other_ary -> new_ary
86 # Set Intersection---Returns a new array
87 # containing elements common to the two arrays, with no duplicates.
89 # [ 1, 1, 3, 5 ] & [ 1, 2, 3 ] #=> [ 1, 3 ]
92 raise TypeError, "can't convert #{elem.class} into Array" unless elem.class == Array
96 elem.each{|v| hash[v] = true }
108 # ary.flatten -> new_ary
109 # ary.flatten(level) -> new_ary
111 # Returns a new array that is a one-dimensional flattening of this
112 # array (recursively). That is, for every element that is an array,
113 # extract its elements into the new array. If the optional
114 # <i>level</i> argument determines the level of recursion to flatten.
116 # s = [ 1, 2, 3 ] #=> [1, 2, 3]
117 # t = [ 4, 5, 6, [7, 8] ] #=> [4, 5, 6, [7, 8]]
118 # a = [ s, t, 9, 10 ] #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
119 # a.flatten #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
120 # a = [ 1, 2, [3, [4, 5] ] ]
121 # a.flatten(1) #=> [1, 2, 3, [4, 5]]
123 def flatten(depth=nil)
126 if e.is_a?(Array) && (depth.nil? || depth > 0)
127 ar += e.flatten(depth.nil? ? nil : depth - 1)
137 # ary.flatten! -> ary or nil
138 # ary.flatten!(level) -> array or nil
140 # Flattens +self+ in place.
141 # Returns <code>nil</code> if no modifications were made (i.e.,
142 # <i>ary</i> contains no subarrays.) If the optional <i>level</i>
143 # argument determines the level of recursion to flatten.
145 # a = [ 1, 2, [3, [4, 5] ] ]
146 # a.flatten! #=> [1, 2, 3, 4, 5]
148 # a #=> [1, 2, 3, 4, 5]
149 # a = [ 1, 2, [3, [4, 5] ] ]
150 # a.flatten!(1) #=> [1, 2, 3, [4, 5]]
152 def flatten!(depth=nil)
156 if e.is_a?(Array) && (depth.nil? || depth > 0)
157 ar += e.flatten(depth.nil? ? nil : depth - 1)
172 # ary.compact -> new_ary
174 # Returns a copy of +self+ with all +nil+ elements removed.
176 # [ "a", nil, "b", nil, "c", nil ].compact
177 # #=> [ "a", "b", "c" ]
187 # ary.compact! -> ary or nil
189 # Removes +nil+ elements from the array.
190 # Returns +nil+ if no changes were made, otherwise returns
193 # [ "a", nil, "b", nil, "c" ].compact! #=> [ "a", "b", "c" ]
194 # [ "a", "b", "c" ].compact! #=> nil
197 result = self.select { |e| e != nil }
198 if result.size == self.size
206 def reverse_each(&block)
207 return to_enum :reverse_each unless block_given?
220 # ary.fetch(index) -> obj
221 # ary.fetch(index, default) -> obj
222 # ary.fetch(index) { |index| block } -> obj
224 # Tries to return the element at position +index+, but throws an IndexError
225 # exception if the referenced +index+ lies outside of the array bounds. This
226 # error can be prevented by supplying a second argument, which will act as a
229 # Alternatively, if a block is given it will only be executed when an
230 # invalid +index+ is referenced. Negative values of +index+ count from the
233 # a = [ 11, 22, 33, 44 ]
236 # a.fetch(4, 'cat') #=> "cat"
237 # a.fetch(100) { |i| puts "#{i} is out of bounds" }
238 # #=> "100 is out of bounds"
241 def fetch(n=nil, ifnone=NONE, &block)
242 warn "block supersedes default value argument" if n != nil && ifnone != NONE && block
248 if idx < 0 || size <= idx
249 return block.call(n) if block
251 raise IndexError, "index #{n} outside of array bounds: #{-size}...#{size}"
260 # ary.fill(obj) -> ary
261 # ary.fill(obj, start [, length]) -> ary
262 # ary.fill(obj, range ) -> ary
263 # ary.fill { |index| block } -> ary
264 # ary.fill(start [, length] ) { |index| block } -> ary
265 # ary.fill(range) { |index| block } -> ary
267 # The first three forms set the selected elements of +self+ (which
268 # may be the entire array) to +obj+.
270 # A +start+ of +nil+ is equivalent to zero.
272 # A +length+ of +nil+ is equivalent to the length of the array.
274 # The last three forms fill the array with the value of the given block,
275 # which is passed the absolute index of each element to be filled.
277 # Negative values of +start+ count from the end of the array, where +-1+ is
280 # a = [ "a", "b", "c", "d" ]
281 # a.fill("x") #=> ["x", "x", "x", "x"]
282 # a.fill("w", -1) #=> ["x", "x", "x", "w"]
283 # a.fill("z", 2, 2) #=> ["x", "x", "z", "z"]
284 # a.fill("y", 0..1) #=> ["y", "y", "z", "z"]
285 # a.fill { |i| i*i } #=> [0, 1, 4, 9]
286 # a.fill(-2) { |i| i*i*i } #=> [0, 1, 8, 27]
287 # a.fill(1, 2) { |i| i+1 } #=> [0, 2, 3, 27]
288 # a.fill(0..1) { |i| i+1 } #=> [1, 2, 3, 27]
291 def fill(arg0=nil, arg1=nil, arg2=nil, &block)
292 if arg0 == nil && arg1 == nil && arg2 == nil && !block
293 raise ArgumentError, "wrong number of arguments (0 for 1..3)"
299 if arg0 == nil && arg1 == nil && arg2 == nil
300 # ary.fill { |index| block } -> ary
303 elsif arg0 != nil && arg0.respond_to?(:begin) && arg0.respond_to?(:end)
304 # ary.fill(range) { |index| block } -> ary
306 beg += self.size if beg < 0
307 len = arg0.end - beg + 1
309 # ary.fill(start [, length] ) { |index| block } -> ary
311 beg += self.size if beg < 0
319 if arg0 != nil && arg1 == nil && arg2 == nil
320 # ary.fill(obj) -> ary
323 elsif arg0 != nil && arg1 != nil && arg1.respond_to?(:begin) && arg1.respond_to?(:end)
324 # ary.fill(obj, range ) -> ary
327 len = arg1.end - beg + 1
328 elsif arg0 != nil && arg1 != nil
329 # ary.fill(obj, start [, length]) -> ary
331 beg += self.size if beg < 0
343 self[i] = block.call(i)