Enumerable#uniq_by

| | Comments (1)

Random code sitting around waiting to be blogged...

module Enumerable
  def uniq_by
    r, s = [], {}
    each do |e|
      v = yield(e)
      next if s[v]
      r << e
      s[v] = true
    end
    r
  end
end

1 Comments

Nice! In case you did not know, uniq accepts a block in 1.9. But I agree the name uniq_by would be more explicit.

Leave a comment

About this Entry

This page contains a single entry by zenspider published on January 18, 2012 3:10 PM.

minitest version 2.10.1 has been released! was the previous entry in this blog.

hoe version 2.13.0 has been released! is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Pages

Powered by Movable Type 4.32-en