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.
Jon (Guest)
on 2006-03-28 06: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
Dorian M. (Guest)
on 2006-03-28 06: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
Ray B. (Guest)
on 2006-03-28 08:57
(Received via mailing list)
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
Ray B. (Guest)
on 2006-03-28 09: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
Nicholas Van W. (Guest)
on 2006-03-28 10:23
(Received via mailing list)
On 3/28/06, Ray B. <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
>


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
François B. (Guest)
on 2006-03-29 02:02
(Received via mailing list)
2006/3/27, Jon <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 !
This topic is locked and can not be replied to.