From: Toby Rodwell Date: 2010-05-14T22:03:21+09:00 Subject: Re: Comparing elements within an array Robert Dober wrote: Many thanks for this, and you ahve exactly described what I am wanting to do. Unfortunately it is throwing up 'undefined method `tap' for nil:NilClass (NoMethodError)'. I tried figuring this out for myself but no joy :( Any ideas? > On Fri, May 14, 2010 at 12:47 PM, Toby Rodwell wrote: > > ah a #unique_by, I therefore conclude that you want to keep one > element with the given property. > > module Enumerable > def unique_by &blk > keys = {} > reject{ | ele | > key = blk[ele] > keys[key].tap{ keys[key]=true } > } > end > end > > p [*1..10].unique_by{ |x| x % 3 } > > Could'nt come up with something more concise :( > HTH > R. -- Posted via http://www.ruby-forum.com/.