From: mgarriss Date: 2003-08-27T02:54:47+09:00 Subject: Re: Array#uniq! Florian Frank wrote: >On 2003-08-27 01:48:14 +0900, mgarriss wrote: > > >>Anyone know why Array#uniq returns an array in every case and >>Array#uniq! returns nil if nothing is removed? >> >> > >Array#uniq! modifies the receiver, you should not rely on the objects >that are returned. However, if the receiver was really changed, uniq! >returns a true (not false and not nil) value. You can code something >like that: > >if ary.uniq! > puts "Made it unique: " + ary.inspect >else > puts "Was already unique: " + ary.inspect >end > >I wonder if it would be better to return an instance of TrueClass >because I think a lot of people will code mistakes like that: > >new_ary = ary.uniq! >new_ary.each do |x| > # ... >end > >Perhaps this works most of the time, but it's just waiting for a >disaster to happen. > Just such a disaster prompted my original post. It turned out that in almost all cases there where duplicates in my array. Then came that one case where there where not. "Hmmm, why does my program that had been working report something about no [] on NilClass? I haven't changed anything." The moral of this story is that I should read the docs on any method I've never used but want to use. ;) Michael