Skip to content
instance method

Enumerable#uniq

() -> ::Array[Elem]
() { (Elem item) -> untyped } -> ::Array[Elem]
uniq → array
uniq {|element| ... } → array

With no block, returns a new array containing only unique elements; the array has no two elements e0 and e1 such that e0.eql?(e1):

Example
%w[a b c c b a a b c].uniq       # => ["a", "b", "c"]
[0, 1, 2, 2, 1, 0, 0, 1, 2].uniq # => [0, 1, 2]

With a block, returns a new array containing elements only for which the block returns a unique value:

Example
a = [0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1]
a.uniq {|i| i.even? ? i : 0 } # => [0, 2, 4]
a = %w[a b c d e e d c b a a b c d e]
a.uniq {|c| c < 'c' }         # => ["a", "c"]
See also #to_set #zip #to_h also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0