ruby - Removing hashes that have identical values for particular keys -


i have array of hashes 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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -