为Jekyll编写一个插件,因为我还不熟悉,所以我被一些ruby相关代码所困扰
如何在阵列上做.select
,.map
及其所有朋友(除了简单的一个数组案例)
所以现在我的问题是,将其作为一系列文档,使用相关的sha1sum
代码:
[
[
#<Jekyll::Document _projects/1st-project.md collection=projects>,
"8f918a3d8263a957c206a9864d3507aaa5277a79"
],
[
#<Jekyll::Document _posts/2nd-project.markdown collection=posts>,
"ca81eda5d49100bdf23db16fe1c9d17040fd33f8"
],
[
#<Jekyll::Document _posts/3rd-project.markdown collection=posts>,
"37bf18464b00c9a808436853bc6d868aff218eb6"
],
...
...
]
另一方面,链接组的哈希就像这样:
{
"linkage_groups"=>
[
{
"group_name"=>"test1",
"sha1_sums"=> [
"ca81eda5d49100bdf23db16fe1c9d17040fd33f8",
"37bf18464b00c9a808436853bc6d868aff218eb6"
]
},
{
"group_name"=>"test1",
"sha1_sums"=> [
"154c255d59e6063fc609ade2254dc1b09d1de8ab",
"3e9ef9f059786888e39df608d104263cf0fdae76"
]
}
]
}
我如何能够一次循环浏览这些组,并从上面的每组sha1_sums
文档中返回
数组,其中文档的sha1_sum
存在于特定组中。
预期输出将是每个组的哈希数组,持有
满足条件的文档,在sha1_sum
出现在一个组中:
实施例。第2和第3个项目符合条件,因为他们的sha's
位于名为test1
[
{
"test1" => [#<Jekyll::Document _posts/2nd-project.markdown collection=posts>, #<Jekyll::Document _posts/3rd-project.markdown collection=posts]
},
{
"test2" => [..., ...]
},
{
"test3" => [..., ...]
},
...
]
截至@Lukas Baliak的回复 -
以下是两个属于同一组的哈希值的结果:
{
"ca81eda5d49100bdf23db16fe1c9d17040fd33f8"=>"test1",
"b673be35ad73ab48da23b271ab0dda95ea07c905"=>"test1",
"154c255d59e6063fc609ade2254dc1b09d1de8ab"=>"test2",
"3e9ef9f059786888e39df608d104263cf0fdae76"=>"test2"
}
[
[
#<Jekyll::Document _projects/my-first-project.md collection=projects>,
"b673be35ad73ab48da23b271ab0dda95ea07c905"
],
[
#<Jekyll::Document _posts/2016-06-05-one-more-post.markdown collection=posts>,
"ca81eda5d49100bdf23db16fe1c9d17040fd33f8"
]
]
{
"test1"=> [#<Jekyll::Document _posts/2016-06-05-one-more-post.markdown collection=posts>, "ca81eda5d49100bdf23db16fe1c9d17040fd33f8"]
}
只列出一个文件,为什么? b673be35ad73ab48da23b271ab0dda95ea07c905
在哪里?
答案 0 :(得分:2)
我不喜欢使用简单的数据结构,所以我将“迁移”到Hash
文档
doc = [
[
"#<Jekyll::Document _projects/my-first-project.md collection=projects>",
"8f918a3d8263a957c206a9864d3507aaa5277a79"
],
[
"#<Jekyll::Document _posts/2016-06-05-one-more-post.markdown collection=posts>",
"ca81eda5d49100bdf23db16fe1c9d17040fd33f8"
]
]
group_config
group_config = {
"linkage_groups" => [
{
"group_name" => "test1",
"sha1_sums" => [
"ca81eda5d49100bdf23db16fe1c9d17040fd33f8",
"b673be35ad73ab48da23b271ab0dda95ea07c905"
]
},
{
"group_name" => "test2",
"sha1_sums" => [
"154c255d59e6063fc609ade2254dc1b09d1de8ab",
"8f918a3d8263a957c206a9864d3507aaa5277a79"
]
}
]
}
迁移到Hash
groups = group_config["linkage_groups"].each_with_object({}) do |h, exp|
h["sha1_sums"].each { |sha1| exp[sha1] = h["group_name"] }
end
导出组
p groups
# {
# "ca81eda5d49100bdf23db16fe1c9d17040fd33f8" => "test1",
# "b673be35ad73ab48da23b271ab0dda95ea07c905" => "test1",
# "154c255d59e6063fc609ade2254dc1b09d1de8ab" => "test2",
# "8f918a3d8263a957c206a9864d3507aaa5277a79" => "test2"
# }
生成哈希结构的过程
export = doc.each_with_object({}) do |arr, exp|
exp[groups[arr[1]]] = arr
end
输出
p export
# {
# "test2" => ["#<Jekyll::Document _projects/my-first-project.md collection=projects>", "8f918a3d8263a957c206a9864d3507aaa5277a79"],
# "test1" => ["#<Jekyll::Document _posts/2016-06-05-one-more-post.markdown collection=posts>", "ca81eda5d49100bdf23db16fe1c9d17040fd33f8"]
# }
修改强>
如果您需要更多,请使用此修改
export = doc.each_with_object(Hash.new{|k, v| k[v] = []}) do |arr, exp|
exp[groups[arr[1]]] << arr
end
编辑2
好的,如果你需要一个sha1_hash到更多的组,你可以使用这个更新。
groups = group_config["linkage_groups"].each_with_object(Hash.new { |k, v| k[v] = [] }) do |h, exp|
h["sha1_sums"].each { |sha1| exp[sha1] << h["group_name"] }
end
基
p groups
# {
# "ca81eda5d49100bdf23db16fe1c9d17040fd33f8" => ["test1"],
# "b673be35ad73ab48da23b271ab0dda95ea07c905" => ["test1"],
# "8f918a3d8263a957c206a9864d3507aaa5277a79" => ["test1", "test2"],
# "154c255d59e6063fc609ade2254dc1b09d1de8ab" => ["test2"]
# }
过程
export = doc.each_with_object(Hash.new { |k, v| k[v] = [] }) do |arr, exp|
groups[arr[1]].each { |group| exp[group] << arr }
end
输出
p export
# {
# "test1" => [
# ["#<Jekyll::Document _projects/my-first-project.md collection=projects>", "8f918a3d8263a957c206a9864d3507aaa5277a79"],
# ["#<Jekyll::Document _posts/2016-06-05-one-more-post.markdown collection=posts>", "ca81eda5d49100bdf23db16fe1c9d17040fd33f8"]
# ],
# "test2" => [
# ["#<Jekyll::Document _projects/my-first-project.md collection=projects>", "8f918a3d8263a957c206a9864d3507aaa5277a79"],
# ["#<Jekyll::Document _posts/2016-06-0s-one-more-post.markdown collection=posts>", "154c255d59e6063fc609ade2254dc1b09d1de8ab"]
# ]
# }
我希望这会有所帮助。
答案 1 :(得分:1)
你的问题不是很清楚。有时,有助于详细说明输入的预期输出。我假设array
包含Document/Sha1
对,而hash
包含linkage_groups
。
hash
看起来像这样:
{"linkage_groups"=>[{"group_name"=>"test1", "sha1_sums"=>["ca81eda5d49100bdf23db16fe1c9d17040fd33f8", "b673be35ad73ab48da23b271ab0dda95ea07c905"]}, {"group_name"=>"test1", "sha1_sums"=>["154c255d59e6063fc609ade2254dc1b09d1de8ab", "3e9ef9f059786888e39df608d104263cf0fdae76"]}]}
array
看起来像这样:
[["Document1", "8f918a3d8263a957c206a9864d3507aaa5277a79"], ["Document2", "ca81eda5d49100bdf23db16fe1c9d17040fd33f8"]]
我会尝试这样的事情:
hash["linkage_groups"].each { |group| // for each linkage group
group["sha1_sums"].each { |sha1| // for each sha1 in group
array.each { |array_element| // for each array element (which itself is an array of document/sha1 pairs
if array_element.include?(sha1) then
puts "#{array_element[0]} found in #{group["group_name"]} with #{group["sha1"]}"
end
}
}
}
我可以根据您的需要管理如何返回元素。