It does everything:
Hash[*a.map{|x| [x.text, x]}].values
short? Yeah.
(an asterisk is optional, it appears to be required for 1.8.6).
For example:
a = [Thing.new('a'), Thing.new('b'), Thing.new('c'), Thing.new('c')] => [
The boring part: here is a small class of tests that I used:
class Thing attr_reader :text def initialize(text) @text = text end def inspect "#<Thing #{text}>" end end
Peter
source share