Forum: Ruby on Rails Anyone use .uniq! ???

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4108c0021c64d188494cae8dcfc95fd8?d=identicon&s=25 Jon (Guest)
on 2006-03-28 04:04
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
F719c6cb34bd5b691c12b373b7244b4d?d=identicon&s=25 Dorian Mcfarland (tachekent)
on 2006-03-28 04:13
(Received via mailing list)
found this in the API docs:
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
E28c35323f624b8b9ed8712e25105454?d=identicon&s=25 Ray Baxter (Guest)
on 2006-03-28 06:57
(Received via mailing list)
Dorian Mcfarland 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
E28c35323f624b8b9ed8712e25105454?d=identicon&s=25 Ray Baxter (Guest)
on 2006-03-28 07:08
(Received via mailing list)
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
902654bac6dff9567f018bd2ed933151?d=identicon&s=25 Nicholas Van Weerdenburg (Guest)
on 2006-03-28 08:23
(Received via mailing list)
On 3/28/06, Ray Baxter <ray@warmroom.com> wrote:
> > => [1, 2, 3, 4, 5]
>
>
> Ray
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> 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
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 François Beausoleil (fbeausoleil)
on 2006-03-29 00:02
(Received via mailing list)
2006/3/27, Jon <jon.druse@gmail.com>:
> 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 !
This topic is locked and can not be replied to.