"websocket", "version"=>"2.8.0-6ren">
gpt4 book ai didi

Ruby:尊重最高版本的独特哈希数组

转载 作者:行者123 更新时间:2023-12-02 18:28:25 24 4
gpt4 key购买 nike

我正在尝试创建一个具有唯一值的新哈希数组,并尊重重复哈希的最高版本。哈希如下所示:

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"}
]

如您所见,第三个、第四个和第五个哈希值具有相同的键dependency 值,即httpartyrepo这是 repo2,但第五个哈希具有这三个哈希中的最高版本。因此,我想创建一个具有第一个、第二个、第五个、第六个和第七个哈希的唯一哈希。所以我想要的结果应该是这样的:

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"}
]

关于版本比较,我想用这个方法来比较一下:

def version_greater? (version1, version2)
Gem::Version.new(version1) > Gem::Version.new(version2)
end

如果版本 1 大于版本 2,则返回 true

如果有任何有助于解决此问题的建议,我将不胜感激。

最佳答案

一种方法是使用 Hash#update 的形式(又名 merge!),需要一个 block (这里 { |_,o,n| n["version"] > o["version"] ? n : o }) 确定正在合并的两个哈希中存在的键的值。

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"},
{"dependency"=>"rails", "version"=>"6.1.9", "repo"=>"repo2"}
]

请注意,我已将哈希添加到问题中显示的 old_hash 中。 (顺便说一句,“old_hash”可能不是数组的最佳名称。)

old_hash.each_with_object({}) do |g,h|
h.update([g["dependency"],g["repo"]]=>g) do |_,o,n|
n["version"] > o["version"] ? n : o
end
end.values
#=> [{"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"}]

的接收者可以看出如下。

  {["websocket", "repo1"] =>{"dependency"=>"websocket",  "version"=>  "2.8.0", "repo"=>"repo1"},
["rails", "repo2"] =>{"dependency"=>"rails", "version"=> "6.2.0", "repo"=>"repo2"},
["httparty", "repo2"] =>{"dependency"=>"httparty", "version"=>"6.1.3.2", "repo"=>"repo2"},
["rails", "repo3"] =>{"dependency"=>"rails", "version"=> "6.1.0", "repo"=>"repo3"},
["metasploit", "repo3"]=>{"dependency"=>"metasploit", "version"=> "2.8.0", "repo"=>"repo3"}}

请参阅文档以获取三个 block 变量的描述:_(公共(public)键,此处使用下划线表示它未在 block 计算中使用)、o,正在构建的哈希中的公共(public) key 的值(认为是“旧”),以及 n,正在合并的哈希中的公共(public) key 的值(认为是"new")。

关于Ruby:尊重最高版本的独特哈希数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69775304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com