Almost at the close of one of the cohort tasks I am working on and I need to uniquify an array of hashes. Many suggestions point to redefining #hash and #eql? for the hash instances before I put them in the array. And voila! there was anoitulos
and I qoute:
"Assuming your hashes are always single key-value pairs, this will work:
a.map {|h| h.to_a[0]}.uniq.map {|k,v| {k => v}}
Hash.to_a creates an array of key-value arrays, so the first map gets you:
[[:a, 1], [:a, 2], [:a, 1]]
uniq on Arrays does what you want, giving you:
[[:a, 1], [:a, 2]]
and then the second map puts them back together as hashes again.
"
Wednesday, August 26, 2009
Tuesday, August 04, 2009
Denzel
Subscribe to:
Posts (Atom)