Skip to content

Instantly share code, notes, and snippets.

@tadman
Created March 26, 2009 18:23
Show Gist options
  • Save tadman/86229 to your computer and use it in GitHub Desktop.
Save tadman/86229 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
class Array
def unique_by
inject(Hash.new(0)) do |h, i|
h[yield(i)] += 1
h
end.size == size
end
end
puts [ 1,2,3,4,5,9 ].unique_by { |n| n % 10 }
puts [ 1,2,3,4,5,9,11 ].unique_by { |n| n % 10 }
puts [ 1,2,3,13,4,5,9 ].unique_by { |n| n % 10 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment