uniq!

ary.uniq! â ary or nil
ary.uniq! { |item| ... } â ary or nil
Instance Public methods

Removes duplicate elements from self.

If a block is given, it will use the return value of the block for comparison.

It compares values using their hash and eql? methods for efficiency.

Returns nil if no changes are made (that is, no duplicates are found).

a = [ "a", "a", "b", "b", "c" ]
a.uniq!   # => ["a", "b", "c"]

b = [ "a", "b", "c" ]
b.uniq!   # => nil

c = [["student","sam"], ["student","george"], ["teacher","matz"]]
c.uniq! { |s| s.first } # => [["student", "sam"], ["teacher", "matz"]]
doc_ruby_on_rails
2015-03-30 21:21:15
Comments
Leave a Comment

Please login to continue.