# Anyone use .uniq!?

**URL:** <https://www.ruby-forum.com/t/anyone-use-uniq/88112>\
**Category:** Rails\
**Created:** [March 28, 2006, 4:04am UTC](https://www.ruby-forum.com/t/anyone-use-uniq/88112 "2006-03-28T04:04:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [March 28, 2006, 4:04am UTC](https://www.ruby-forum.com/t/anyone-use-uniq/88112/1 "2006-03-28T04:04:47Z")

</div>

could anyone by chance give me a working example of the .uniq! method?  
i’ve been trying all day. any help would be much appreciated!

jon

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [March 28, 2006, 4:13am UTC](https://www.ruby-forum.com/t/anyone-use-uniq/88112/2 "2006-03-28T04:13:57Z")

</div>

found this in the API docs:  
[http://www.rubycentral.com/ref/ref\_c\_array.html#uniq\_oh](http://www.rubycentral.com/ref/ref_c_array.html#uniq_oh)

make sure you’re using it on an array, and remember that if no  
duplicates are found, the result is nil.

dorian

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [March 28, 2006, 6:57am UTC](https://www.ruby-forum.com/t/anyone-use-uniq/88112/3 "2006-03-28T06:57:12Z")

</div>

Dorian M. wrote:

> > i’ve been trying all day. any help would be much appreciated!  
> > irb(main):001:0\> a = [1, 2, 3, 3, 4, 5]  
> > =\> [1, 2, 3, 3, 4, 5]  
> > irb(main):002:0\> a.uniq  
> > =\> [1, 2, 3, 4, 5]  
> > irb(main):003:0\> a  
> > =\> [1, 2, 3, 3, 4, 5]  
> > irb(main):004:0\> a.uniq!  
> > =\> [1, 2, 3, 4, 5]  
> > irb(main):005:0\> a  
> > =\> [1, 2, 3, 4, 5]  
> > irb(main):006:0\> a.uniq!  
> > =\> nil

Ray

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [March 28, 2006, 7:08am UTC](https://www.ruby-forum.com/t/anyone-use-uniq/88112/4 "2006-03-28T07:08:24Z")

</div>

I missed an important bit

> =\> [1, 2, 3, 3, 4, 5]  
> irb(main):004:0\> a.uniq!  
> =\> [1, 2, 3, 4, 5]  
> irb(main):005:0\> a  
> =\> [1, 2, 3, 4, 5]  
> irb(main):006:0\> a.uniq!  
> =\> nil  
> irb(main):007:0\> a  
> =\> [1, 2, 3, 4, 5]

Ray

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [March 28, 2006, 8:23am UTC](https://www.ruby-forum.com/t/anyone-use-uniq/88112/5 "2006-03-28T08:23:59Z")

</div>

On 3/28/06, Ray B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > =\> [1, 2, 3, 4, 5]
> 
> Ray
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

This is exactly according to the documentation. compact and compact!  
work  
the same way-

## ri uniq ------------------------------------------------------------- Array#uniq array.uniq → an\_array

```
 Returns a new array by removing duplicate values in _self_.

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

```

## ri uniq! ------------------------------------------------------------ Array#uniq! array.uniq! → array or nil

```
 Removes duplicate elements from _self_. 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

```

Regards,  
Nick

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [March 29, 2006, 12:02am UTC](https://www.ruby-forum.com/t/anyone-use-uniq/88112/6 "2006-03-29T00:02:45Z")

</div>

2006/3/27, Jon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> could anyone by chance give me a working example of the .uniq! method?  
> i’ve been trying all day. any help would be much appreciated!

array = %w(a a)  
puts array  
[“a”, “b”]  
array.uniq!  
puts array  
[“a”]

#uniq! makes the receiver keep unique values. Values are compared  
using #== (or is it #equal?). #uniq! is equivalent to this loop:

temp = Array.new  
%w(a a).each do |value|  
temp \<\< value unless temp.include?(value)  
end

Of course, this loop returns a NEW array, whereas #uniq! changes the  
receiver (notice ! at the end ?)

Hope that helps !
