4 # ary.uniq! -> ary or nil
5 # ary.uniq! { |item| ... } -> ary or nil
7 # Removes duplicate elements from +self+.
8 # Returns <code>nil</code> if no changes are made (that is, no
9 # duplicates are found).
11 # a = [ "a", "a", "b", "b", "c" ]
12 # a.uniq! #=> ["a", "b", "c"]
13 # b = [ "a", "b", "c" ]
15 # c = [["student","sam"], ["student","george"], ["teacher","matz"]]
16 # c.uniq! { |s| s.first } # => [["student", "sam"], ["teacher", "matz"]]
26 hash[key] = val unless hash.has_key?(key)
28 hash.each_value do |value|
34 ary.delete(result.last)
37 if result.size == self.size
47 # ary.uniq { |item| ... } -> new_ary
49 # Returns a new array by removing duplicate values in +self+.
51 # a = [ "a", "a", "b", "b", "c" ]
52 # a.uniq #=> ["a", "b", "c"]
54 # b = [["student","sam"], ["student","george"], ["teacher","matz"]]
55 # b.uniq { |s| s.first } # => [["student", "sam"], ["teacher", "matz"]]
69 # ary - other_ary -> new_ary
71 # Array Difference---Returns a new array that is a copy of
72 # the original array, removing any items that also appear in
73 # <i>other_ary</i>. (If you need set-like behavior, see the
76 # [ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ] #=> [ 3, 3, 5 ]
79 raise TypeError, "can't convert #{elem.class} into Array" unless elem.class == Array
83 elem.each { |x| hash[x] = true }
84 self.each { |x| array << x unless hash[x] }
90 # ary | other_ary -> new_ary
92 # Set Union---Returns a new array by joining this array with
93 # <i>other_ary</i>, removing duplicates.
95 # [ "a", "b", "c" ] | [ "c", "d", "a" ]
96 # #=> [ "a", "b", "c", "d" ]
99 raise TypeError, "can't convert #{elem.class} into Array" unless elem.class == Array
107 # ary & other_ary -> new_ary
109 # Set Intersection---Returns a new array
110 # containing elements common to the two arrays, with no duplicates.
112 # [ 1, 1, 3, 5 ] & [ 1, 2, 3 ] #=> [ 1, 3 ]
115 raise TypeError, "can't convert #{elem.class} into Array" unless elem.class == Array
119 elem.each{|v| hash[v] = true }
131 # ary.flatten -> new_ary
132 # ary.flatten(level) -> new_ary
134 # Returns a new array that is a one-dimensional flattening of this
135 # array (recursively). That is, for every element that is an array,
136 # extract its elements into the new array. If the optional
137 # <i>level</i> argument determines the level of recursion to flatten.
139 # s = [ 1, 2, 3 ] #=> [1, 2, 3]
140 # t = [ 4, 5, 6, [7, 8] ] #=> [4, 5, 6, [7, 8]]
141 # a = [ s, t, 9, 10 ] #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
142 # a.flatten #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
143 # a = [ 1, 2, [3, [4, 5] ] ]
144 # a.flatten(1) #=> [1, 2, 3, [4, 5]]
146 def flatten(depth=nil)
149 if e.is_a?(Array) && (depth.nil? || depth > 0)
150 ar += e.flatten(depth.nil? ? nil : depth - 1)
160 # ary.flatten! -> ary or nil
161 # ary.flatten!(level) -> array or nil
163 # Flattens +self+ in place.
164 # Returns <code>nil</code> if no modifications were made (i.e.,
165 # <i>ary</i> contains no subarrays.) If the optional <i>level</i>
166 # argument determines the level of recursion to flatten.
168 # a = [ 1, 2, [3, [4, 5] ] ]
169 # a.flatten! #=> [1, 2, 3, 4, 5]
171 # a #=> [1, 2, 3, 4, 5]
172 # a = [ 1, 2, [3, [4, 5] ] ]
173 # a.flatten!(1) #=> [1, 2, 3, [4, 5]]
175 def flatten!(depth=nil)
179 if e.is_a?(Array) && (depth.nil? || depth > 0)
180 ar += e.flatten(depth.nil? ? nil : depth - 1)
195 # ary.compact -> new_ary
197 # Returns a copy of +self+ with all +nil+ elements removed.
199 # [ "a", nil, "b", nil, "c", nil ].compact
200 # #=> [ "a", "b", "c" ]
210 # ary.compact! -> ary or nil
212 # Removes +nil+ elements from the array.
213 # Returns +nil+ if no changes were made, otherwise returns
216 # [ "a", nil, "b", nil, "c" ].compact! #=> [ "a", "b", "c" ]
217 # [ "a", "b", "c" ].compact! #=> nil
220 result = self.select { |e| e != nil }
221 if result.size == self.size
229 def reverse_each(&block)
230 return to_enum :reverse_each unless block_given?
243 # ary.fetch(index) -> obj
244 # ary.fetch(index, default) -> obj
245 # ary.fetch(index) { |index| block } -> obj
247 # Tries to return the element at position +index+, but throws an IndexError
248 # exception if the referenced +index+ lies outside of the array bounds. This
249 # error can be prevented by supplying a second argument, which will act as a
252 # Alternatively, if a block is given it will only be executed when an
253 # invalid +index+ is referenced. Negative values of +index+ count from the
256 # a = [ 11, 22, 33, 44 ]
259 # a.fetch(4, 'cat') #=> "cat"
260 # a.fetch(100) { |i| puts "#{i} is out of bounds" }
261 # #=> "100 is out of bounds"
264 def fetch(n=nil, ifnone=NONE, &block)
265 warn "block supersedes default value argument" if n != nil && ifnone != NONE && block
271 if idx < 0 || size <= idx
272 return block.call(n) if block
274 raise IndexError, "index #{n} outside of array bounds: #{-size}...#{size}"
283 # ary.fill(obj) -> ary
284 # ary.fill(obj, start [, length]) -> ary
285 # ary.fill(obj, range ) -> ary
286 # ary.fill { |index| block } -> ary
287 # ary.fill(start [, length] ) { |index| block } -> ary
288 # ary.fill(range) { |index| block } -> ary
290 # The first three forms set the selected elements of +self+ (which
291 # may be the entire array) to +obj+.
293 # A +start+ of +nil+ is equivalent to zero.
295 # A +length+ of +nil+ is equivalent to the length of the array.
297 # The last three forms fill the array with the value of the given block,
298 # which is passed the absolute index of each element to be filled.
300 # Negative values of +start+ count from the end of the array, where +-1+ is
303 # a = [ "a", "b", "c", "d" ]
304 # a.fill("x") #=> ["x", "x", "x", "x"]
305 # a.fill("w", -1) #=> ["x", "x", "x", "w"]
306 # a.fill("z", 2, 2) #=> ["x", "x", "z", "z"]
307 # a.fill("y", 0..1) #=> ["y", "y", "z", "z"]
308 # a.fill { |i| i*i } #=> [0, 1, 4, 9]
309 # a.fill(-2) { |i| i*i*i } #=> [0, 1, 8, 27]
310 # a.fill(1, 2) { |i| i+1 } #=> [0, 2, 3, 27]
311 # a.fill(0..1) { |i| i+1 } #=> [1, 2, 3, 27]
314 def fill(arg0=nil, arg1=nil, arg2=nil, &block)
315 if arg0 == nil && arg1 == nil && arg2 == nil && !block
316 raise ArgumentError, "wrong number of arguments (0 for 1..3)"
322 if arg0 == nil && arg1 == nil && arg2 == nil
323 # ary.fill { |index| block } -> ary
326 elsif arg0 != nil && arg0.respond_to?(:begin) && arg0.respond_to?(:end)
327 # ary.fill(range) { |index| block } -> ary
329 beg += self.size if beg < 0
330 len = arg0.end - beg + 1
332 # ary.fill(start [, length] ) { |index| block } -> ary
334 beg += self.size if beg < 0
342 if arg0 != nil && arg1 == nil && arg2 == nil
343 # ary.fill(obj) -> ary
346 elsif arg0 != nil && arg1 != nil && arg1.respond_to?(:begin) && arg1.respond_to?(:end)
347 # ary.fill(obj, range ) -> ary
350 len = arg1.end - beg + 1
351 elsif arg0 != nil && arg1 != nil
352 # ary.fill(obj, start [, length]) -> ary
354 beg += self.size if beg < 0
366 self[i] = block.call(i)
380 # ary.rotate(count=1) -> new_ary
382 # Returns a new array by rotating +self+ so that the element at +count+ is
383 # the first element of the new array.
385 # If +count+ is negative then it rotates in the opposite direction, starting
386 # from the end of +self+ where +-1+ is the last element.
388 # a = [ "a", "b", "c", "d" ]
389 # a.rotate #=> ["b", "c", "d", "a"]
390 # a #=> ["a", "b", "c", "d"]
391 # a.rotate(2) #=> ["c", "d", "a", "b"]
392 # a.rotate(-3) #=> ["b", "c", "d", "a"]
399 idx = (count < 0) ? (len - (~count % len) - 1) : (count % len) # rotate count
403 idx = 0 if idx > len-1
411 # ary.rotate!(count=1) -> ary
413 # Rotates +self+ in place so that the element at +count+ comes first, and
416 # If +count+ is negative then it rotates in the opposite direction, starting
417 # from the end of the array where +-1+ is the last element.
419 # a = [ "a", "b", "c", "d" ]
420 # a.rotate! #=> ["b", "c", "d", "a"]
421 # a #=> ["b", "c", "d", "a"]
422 # a.rotate!(2) #=> ["d", "a", "b", "c"]
423 # a.rotate!(-3) #=> ["a", "b", "c", "d"]
426 self.replace(self.rotate(count))