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.kind_of?(Range)
327 # ary.fill(range) { |index| block } -> ary
329 beg += self.size if beg < 0
331 len += self.size if len < 0
332 len += 1 unless arg0.exclude_end?
334 # ary.fill(start [, length] ) { |index| block } -> ary
336 beg += self.size if beg < 0
344 if !arg0.nil? && arg1.nil? && arg2.nil?
345 # ary.fill(obj) -> ary
348 elsif !arg0.nil? && !arg1.nil? && arg1.kind_of?(Range)
349 # ary.fill(obj, range ) -> ary
351 beg += self.size if beg < 0
353 len += self.size if len < 0
354 len += 1 unless arg1.exclude_end?
355 elsif !arg0.nil? && !arg1.nil?
356 # ary.fill(obj, start [, length]) -> ary
358 beg += self.size if beg < 0
370 self[i] = block.call(i)
384 # ary.rotate(count=1) -> new_ary
386 # Returns a new array by rotating +self+ so that the element at +count+ is
387 # the first element of the new array.
389 # If +count+ is negative then it rotates in the opposite direction, starting
390 # from the end of +self+ where +-1+ is the last element.
392 # a = [ "a", "b", "c", "d" ]
393 # a.rotate #=> ["b", "c", "d", "a"]
394 # a #=> ["a", "b", "c", "d"]
395 # a.rotate(2) #=> ["c", "d", "a", "b"]
396 # a.rotate(-3) #=> ["b", "c", "d", "a"]
403 idx = (count < 0) ? (len - (~count % len) - 1) : (count % len) # rotate count
407 idx = 0 if idx > len-1
415 # ary.rotate!(count=1) -> ary
417 # Rotates +self+ in place so that the element at +count+ comes first, and
420 # If +count+ is negative then it rotates in the opposite direction, starting
421 # from the end of the array where +-1+ is the last element.
423 # a = [ "a", "b", "c", "d" ]
424 # a.rotate! #=> ["b", "c", "d", "a"]
425 # a #=> ["b", "c", "d", "a"]
426 # a.rotate!(2) #=> ["d", "a", "b", "c"]
427 # a.rotate!(-3) #=> ["a", "b", "c", "d"]
430 self.replace(self.rotate(count))
435 # ary.delete_if { |item| block } -> ary
436 # ary.delete_if -> Enumerator
438 # Deletes every element of +self+ for which block evaluates to +true+.
440 # The array is changed instantly every time the block is called, not after
441 # the iteration is over.
443 # See also Array#reject!
445 # If no block is given, an Enumerator is returned instead.
447 # scores = [ 97, 42, 75 ]
448 # scores.delete_if {|score| score < 80 } #=> [97]
450 def delete_if(&block)
451 return to_enum :delete_if unless block_given?
454 while idx < self.size do
455 if block.call(self[idx])
466 # ary.reject! { |item| block } -> ary or nil
467 # ary.reject! -> Enumerator
469 # Equivalent to Array#delete_if, deleting elements from +self+ for which the
470 # block evaluates to +true+, but returns +nil+ if no changes were made.
472 # The array is changed instantly every time the block is called, not after
473 # the iteration is over.
475 # See also Enumerable#reject and Array#delete_if.
477 # If no block is given, an Enumerator is returned instead.
480 return to_enum :reject! unless block_given?
484 while idx < self.size do
485 if block.call(self[idx])
500 # ary.insert(index, obj...) -> ary
502 # Inserts the given values before the element with the given +index+.
504 # Negative indices count backwards from the end of the array, where +-1+ is
508 # a.insert(2, 99) #=> ["a", "b", 99, "c", "d"]
509 # a.insert(-2, 1, 2, 3) #=> ["a", "b", 99, "c", 1, 2, 3, "d"]
511 def insert(idx, *args)
512 idx += self.size + 1 if idx < 0
519 # ary.bsearch {|x| block } -> elem
521 # By using binary search, finds a value from this array which meets
522 # the given condition in O(log n) where n is the size of the array.
524 # You can use this method in two use cases: a find-minimum mode and
525 # a find-any mode. In either case, the elements of the array must be
526 # monotone (or sorted) with respect to the block.
528 # In find-minimum mode (this is a good choice for typical use case),
529 # the block must return true or false, and there must be an index i
530 # (0 <= i <= ary.size) so that:
532 # - the block returns false for any element whose index is less than
534 # - the block returns true for any element whose index is greater
535 # than or equal to i.
537 # This method returns the i-th element. If i is equal to ary.size,
540 # ary = [0, 4, 7, 10, 12]
541 # ary.bsearch {|x| x >= 4 } #=> 4
542 # ary.bsearch {|x| x >= 6 } #=> 7
543 # ary.bsearch {|x| x >= -1 } #=> 0
544 # ary.bsearch {|x| x >= 100 } #=> nil
546 # In find-any mode (this behaves like libc's bsearch(3)), the block
547 # must return a number, and there must be two indices i and j
548 # (0 <= i <= j <= ary.size) so that:
550 # - the block returns a positive number for ary[k] if 0 <= k < i,
551 # - the block returns zero for ary[k] if i <= k < j, and
552 # - the block returns a negative number for ary[k] if
555 # Under this condition, this method returns any element whose index
556 # is within i...j. If i is equal to j (i.e., there is no element
557 # that satisfies the block), this method returns nil.
559 # ary = [0, 4, 7, 10, 12]
560 # # try to find v such that 4 <= v < 8
561 # ary.bsearch {|x| 1 - (x / 4).truncate } #=> 4 or 7
562 # # try to find v such that 8 <= v < 10
563 # ary.bsearch {|x| 4 - (x / 2).truncate } #=> nil
565 # You must not mix the two modes at a time; the block must always
566 # return either true/false, or always return a number. It is
567 # undefined which value is actually picked up at each iteration.
570 return to_enum :bsearch unless block_given?
576 mid = low + ((high - low) / 2).truncate
585 elsif v == false || v.nil?
594 return nil if low == self.size
595 return nil unless satisfied
601 # ary.delete_if { |item| block } -> ary
602 # ary.delete_if -> Enumerator
604 # Deletes every element of +self+ for which block evaluates to +true+.
606 # The array is changed instantly every time the block is called, not after
607 # the iteration is over.
609 # See also Array#reject!
611 # If no block is given, an Enumerator is returned instead.
613 # scores = [ 97, 42, 75 ]
614 # scores.delete_if {|score| score < 80 } #=> [97]
616 def delete_if(&block)
617 return to_enum :delete_if unless block_given?
620 while idx < self.size do
621 if block.call(self[idx])
632 # ary.keep_if { |item| block } -> ary
633 # ary.keep_if -> Enumerator
635 # Deletes every element of +self+ for which the given block evaluates to
638 # See also Array#select!
640 # If no block is given, an Enumerator is returned instead.
642 # a = [1, 2, 3, 4, 5]
643 # a.keep_if { |val| val > 3 } #=> [4, 5]
646 return to_enum :keep_if unless block_given?
650 while idx < self.size do
651 if block.call(self[idx])
662 # ary.select! {|item| block } -> ary or nil
663 # ary.select! -> Enumerator
665 # Invokes the given block passing in successive elements from +self+,
666 # deleting elements for which the block returns a +false+ value.
668 # If changes were made, it will return +self+, otherwise it returns +nil+.
670 # See also Array#keep_if
672 # If no block is given, an Enumerator is returned instead.
675 return to_enum :select! unless block_given?
679 result << x if block.call(x)
681 return nil if self.size == result.size
687 # ary.index(val) -> int or nil
688 # ary.index {|item| block } -> int or nil
690 # Returns the _index_ of the first object in +ary+ such that the object is
691 # <code>==</code> to +obj+.
693 # If a block is given instead of an argument, returns the _index_ of the
694 # first object for which the block returns +true+. Returns +nil+ if no
698 def index(val=NONE, &block)
699 return to_enum(:find_index, val) if !block && val == NONE
704 return idx if block.call(*e)
708 return self.__ary_index(val)