"Robert Klemme" <bob.news / gmx.net> schrieb im Newsbeitrag news:35q3t9F4jlog7U1 / individual.net... > > "Belorion" <belorion / gmail.com> schrieb im Newsbeitrag > news:a48d774d05012607334cd93e1e / mail.gmail.com... >>I have an array of arrays. I want to be able to do a uniq operation >> on my array, and have it accept a block so I can specify what to >> examine for uniqueness. For example, in IRB >> >> a = [ [1,2], [3,4], [1,3] ] >> a.sort { |x,y| x[0] <=> y[0] } >> >> returns => [ [1,2], [1,3], [3,4] ] >> >> However, it does not appear uniq accepts blocks: >> >> a = [ [1,2], [3,4], [1,3] ] >> a.uniq { |x,y| x[0] == y[0] } >> >> returns => [ [1,2], [3,4], [1,3] ] >> and a.uniq! with the above block returns nil. >> >> Am I stuck writing my own deep_uniq? Granted, it would be hard, but >> certainly not as clean as the above. > > The block is accepted but apparently not used: > >>> %w{a b c d a d}.uniq {|*x| p x} > => ["a", "b", "c", "d"] > > Could be a bug in the std lib. (Btw, I'm on 1.8.1 here) Delete that: you can provide a block for *every* method - it's simply ignored. So no bug, but unique obviously is not built to use it. Stupid me... robert > > How about using a Hash in the meantime? > >>> h = a.inject({}){|h,(k,v)| h[k] ||= [k,v];h} > => {1=>[1, 2], 3=>[3, 4]} >>> h.values > => [[1, 2], [3, 4]] > > Or > >>> h = a.inject({}){|h,pair| h[pair[0]] ||= pair;h} > => {1=>[1, 2], 3=>[3, 4]} >>> h.values > => [[1, 2], [3, 4]] > > Kind regards > > robert >