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