Array#uniq has this behaviour in Ruby 1.9
c = [ "a:def", "a:xyz", "b:abc", "b:xyz", "c:jkl" ]
c.uniq {|s| s[/^\w+/]} #=> [ "a:def", "b:abc", "c:jkl" ]
It can take a block and give unique value with respect to what we give. But, this wont work in Ruby 1.8. How can I create this functionality in ruby 1.8?
Install Marc-André LaFortune's backports gem:
That has the block versions of 1.9.2's
Array#uniq
andArray#uniq!
. Or if you don't want or need the whole thing, the parts are pretty well isolated so you can pull out just the pieces you need:It's easy to implement something like: