instance method
Enumerable#uniq
() -> ::Array[Elem]
() { (Elem item) -> untyped } -> ::Array[Elem]
uniq → array
uniq {|element| ... } → array
enum.c
View on GitHub ↗
static VALUE
enum_uniq(VALUE obj)
{
VALUE hash, ret;
rb_block_call_func *const func =
rb_block_given_p() ? uniq_iter : uniq_func;
hash = rb_obj_hide(rb_hash_new());
rb_block_call(obj, id_each, 0, 0, func, hash);
ret = rb_hash_values(hash);
rb_hash_clear(hash);
return ret;
}
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"]