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 -- Posted via http://www.ruby-forum.com/.
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 Jon wrote:> 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 >-- -- I do things for love or money
Dorian Mcfarland wrote:> 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 > > Jon wrote: >> 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!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
I missed an important bit>> Jon wrote: >>> 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! > > > 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! > => nilirb(main):007:0> a => [1, 2, 3, 4, 5] Ray
On 3/28/06, Ray Baxter <ray@warmroom.com> wrote:> > I missed an important bit > >> Jon wrote: > >>> 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! > > > > > > 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 > irb(main):007:0> a > => [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 -- Nicholas Van Weerdenburg -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060328/aa3f4662/attachment.html
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 ! -- Fran?ois Beausoleil http://blog.teksol.info/