ruby - Removing hashes that have identical values for particular keys -
i have array
of hash
es same keys, storing people's data.
i want remove hashes have same values keys :name
, :surname
. rest of values can differ, calling uniq!
on array won't work.
is there simple solution this?
you can pass block uniq
or uniq!
, value returned block used compare 2 entries equality:
irb> people = [{name: 'foo', surname: 'bar', age: 10}, {name: 'foo', surname: 'bar' age: 11}] irb> people.uniq { |p| [p[:name], p[:surname]] } => [{:name=>"foo", :surname=>"bar", :age=>10}]
Comments
Post a Comment