I'm trying to create a new array of hashes with unique values and with respecting the highest version of repeated hashes. The hash looks like the following:
old_hash = [
{"dependency"=>"websocket", "version"=>"2.8.0", "repo"=>"repo1"},
{"dependency"=>"rails", "version"=>"6.2.0", "repo"=>"repo2"},
{"dependency"=>"httparty", "version"=>"6.0.3.5", "repo"=>"repo2"},
{"dependency"=>"httparty", "version"=>"6.1.0.2", "repo"=>"repo2"},
{"dependency"=>"httparty", "version"=>"6.1.3.2", "repo"=>"repo2"},
{"dependency"=>"rails", "version"=>"6.1.0", "repo"=>"repo3"},
{"dependency"=>"metasploit", "version"=>"2.8.0", "repo"=>"repo3"}
]
As you can see, the third, fourth, and fifth hash has the same value of key dependency which is httparty and also repo which is repo2, but the fifth hash has the highest version of these three. Therefore, I'd like to create a unique hash that has the first, second, fifth, sixth, and seventh hash. So the result I'm trying to have should look like this:
unique_hash = [
{"dependency"=>"websocket", "version"=>"2.8.0", "repo"=>"repo1"},
{"dependency"=>"rails", "version"=>"6.2.0", "repo"=>"repo2"},
{"dependency"=>"httparty", "version"=>"6.1.3.2", "repo"=>"repo2"},
{"dependency"=>"rails", "version"=>"6.1.0", "repo"=>"repo3"},
{"dependency"=>"metasploit", "version"=>"2.8.0", "repo"=>"repo3"}
]
Regarding the version comparison, I'm thinking of using this method to compare them right:
def version_greater? (version1, version2)
Gem::Version.new(version1) > Gem::Version.new(version2)
end
which returns true in case version1 is greater than version2.
I would appreciate any suggestions that helps to solve this problem.