Skip to content

Instantly share code, notes, and snippets.

@kohgpat
Created April 25, 2012 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kohgpat/2490825 to your computer and use it in GitHub Desktop.
Save kohgpat/2490825 to your computer and use it in GitHub Desktop.
Remove dups from array by key
Hash[*a.map{|x| [x.text, x]}].values
a = [Thing.new('a'), Thing.new('b'), Thing.new('c'), Thing.new('c')]
# => [#<Thing a>, #<Thing b>, #<Thing c>, #<Thing c>]
Hash[a.map{|x| [x.text, x]}].values
# => [#<Thing a>, #<Thing b>, #<Thing c>]
class Thing
attr_reader :text
def initialize(text)
@text = text
end
def inspect
"#<Thing #{text}>"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment