I have a hash with items like
我有一个哈希的项目,如
{'people'=>'50'},
{'chairs'=>'23'},
{'footballs'=>'5'},
{'crayons'=>'1'},
and I have another hash with
我有另一个哈希
{'people'=>'http://www.thing.com/this-post'},
{'footballs'=>'http://www.thing.com/that-post'},
{'people'=>'http://www.thing.com/nice-post'},
{'footballs'=>'http://www.thing.com/other-post'},
{'people'=>'http://www.thing.com/thingy-post'},
{'footballs'=>'http://www.thing.com/the-post'},
{'people'=>'http://www.thing.com/the-post'},
{'crayons'=>'http://www.thing.com/the-blah'},
{'chairs'=>'http://www.thing.com/the-page'},
and I want something like the following that takes the first hash and then looks through the second one, grabs all the links for each word and puts them into a array appended onto the end of the hash somehow.
我希望像下面这样的东西采用第一个哈希然后查看第二个哈希,抓住每个单词的所有链接并将它们放入一个附加到哈希末尾的数组中。
{'people', '50' => {'http://www.thing.com/this-post', 'http://www.thing.com/nice-post', 'http://www.thing.com/thingy-post'}},
{'footballs', '5' => {'http://www.thing.com/the-post', 'http://www.thing.com/the-post'}},
{'crayons', '1' => {'http://www.thing.com/the-blah'}},
{'chairs', '23' => {'chairs'=>'http://www.thing.com/the-page'}},
I am very new to Ruby, and I have tried quite a few combinations, and I need some help.
我是Ruby的新手,我尝试了很多组合,我需要一些帮助。
Excuse the example, I hope that it makes sense.
请原谅这个例子,我希望这是有道理的。
3 个解决方案
#1
1
Since there is some confusion about the format of the data, I will suggest how you might effectively structure both the input and the output. I will first present some code you could use, then give an example of how it's used, then explain what is happening.
由于对数据格式存在一些混淆,我将建议您如何有效地构建输入和输出。我将首先介绍一些您可以使用的代码,然后举例说明它是如何使用的,然后解释发生了什么。
Code
码
def merge_em(hash, array)
hash_keys = hash.keys
new_hash = hash_keys.each_with_object({}) { |k,h|
h[k] = { qty: hash[k], http: [] } }
array.each do |h|
h.keys.each do |k|
(new_hash.update({k=>h[k]}) { |k,g,http|
{qty: g[:qty], http: (g[:http] << http)}}) if hash_keys.include?(k)
end
end
new_hash
end
Example
例
Here is a hash that I have modified to include a key/value pair that does not appear in the array below:
这是一个哈希,我已修改为包含未出现在下面数组中的键/值对:
hash = {'people' =>'50', 'chairs' =>'23', 'footballs'=>'5',
'crayons'=> '1', 'cat_lives'=> '9'}
Below is your array of hashes that is to be merged into hash
. You'll see I've added a key/value pair to your hash with key "chairs"
. As I hope to make clear, the code is no different (i.e., not simplified) if we know in advance that each hash has only one key value pair. (Aside: if, for example, we want want the key from a hash h
that is known to have only one key, we still have to pull out all the keys into an array and then take the only element of the array: h.keys.first
).
下面是要合并为哈希的哈希数组。你会看到我已经使用键“chair”为你的哈希添加了一个键/值对。正如我希望的那样,如果我们事先知道每个散列只有一个键值对,则代码没有区别(即,不简化)。 (旁白:例如,如果我们想要一个已知只有一个密钥的哈希h中的密钥,我们仍然必须将所有密钥拉出到一个数组中,然后取出该数组的唯一元素:h。 keys.first)。
I have also added a hash to the array that has no key that is among hash
's keys.
我还在数组中添加了一个哈希,该哈希没有哈希键中的键。
array =
[{'people' =>'http://www.thing.com/this-post'},
{'footballs'=>'http://www.thing.com/that-post'},
{'people' =>'http://www.thing.com/nice-post'},
{'footballs'=>'http://www.thing.com/other-post'},
{'people' =>'http://www.thing.com/thingy-post'},
{'footballs'=>'http://www.thing.com/the-post'},
{'people' =>'http://www.thing.com/the-post'},
{'crayons' =>'http://www.thing.com/the-blah'},
{'chairs' =>'http://www.thing.com/the-page',
'crayons' =>'http://www.thing.com/blah'},
{'balloons' =>'http://www.thing.com/the-page'}
]
We now merge the information from array
into hash
, and at the same time change the structure of hash
to something more suitable:
我们现在将数组中的信息合并到散列中,同时将散列结构更改为更合适的内容:
result = merge_em(hash, array)
#=> {"people" =>{:qty=>"50",
# :http=>["http://www.thing.com/this-post",
# "http://www.thing.com/nice-post",
# "http://www.thing.com/thingy-post",
# "http://www.thing.com/the-post"]},
# "chairs" =>{:qty=>"23",
# :http=>["http://www.thing.com/the-page"]},
# "footballs"=>{:qty=>"5",
# :http=>["http://www.thing.com/that-post",
# "http://www.thing.com/other-post",
# "http://www.thing.com/the-post"]},
# "crayons" =>{:qty=>"1",
# :http=>["http://www.thing.com/the-blah",
# "http://www.thing.com/blah"]},
# "cat_lives"=>{:qty=>"9",
# :http=>[]}}
I've assumed you want to look up the content of result
with hash
's keys. It is therefore convenient to make the values associated with those keys hashes themselves, with keys :qty
and http
. The former is for the values in hash
(the naming may be wrong); the latter is an array containing the strings drawn from array
.
我假设你想用哈希的键查找结果的内容。因此,使用键:qty和http使得与这些键相关联的值自身散列是很方便的。前者是哈希值(命名可能是错误的);后者是一个包含从数组中绘制的字符串的数组。
This way, if we want the value for the key "crayons"
, we could write:
这样,如果我们想要键“蜡笔”的值,我们可以写:
result["crayons"]
#=> {:qty=>"1",
# :http=>["http://www.thing.com/the-blah", "http://www.thing.com/blah"]}
or
要么
irb(main):133:0> result["crayons"][:qty]
#=> "1"
irb(main):134:0> result["crayons"][:http]
#=> ["http://www.thing.com/the-blah", "http://www.thing.com/blah"]
Explanation
说明
Let's go through this line-by-line. First, we need to reference hash.keys
more than once, so let's make it a variable:
让我们逐行完成。首先,我们需要多次引用hash.keys,所以让它变成一个变量:
hash_keys = hash.keys
#=> ["people", "chairs", "footballs", "crayons", "cat_lives"]
We may as well convert this hash to the output format now. We could do it during the merge operation below, but I think is clearer to do it as a separate step:
我们现在也可以将此哈希转换为输出格式。我们可以在下面的合并操作中执行此操作,但我认为将其作为单独的步骤更清楚:
new_hash = hash_keys.each_with_object({}) { |k,h|
h[k] = { qty: hash[k], http: [] } }
#=> {"people" =>{:qty=>"50", :http=>[]},
# "chairs" =>{:qty=>"23", :http=>[]},
# "footballs"=>{:qty=>"5", :http=>[]},
# "crayons" =>{:qty=>"1", :http=>[]},
# "cat_lives"=>{:qty=>"9", :http=>[]}}
Now we merge each (hash) element of array
into new_hash
:
现在我们将数组的每个(哈希)元素合并到new_hash中:
array.each do |h|
h.keys.each do |k|
(new_hash.update({k=>h[k]}) { |k,g,http|
{ qty: g[:qty], http: (g[:http] << http) } }) if hash_keys.include?(k)
end
end
The first hash h
from array
that is passed into the block by each
is:
每个传递给块的数组中的第一个哈希值是:
{'people'=>'http://www.thing.com/this-post'}
which is assigned to the block variable h
. We next construct an array of h
's keys:
分配给块变量h。接下来我们构造一个h键的数组:
h.keys #=> ["people"]
The first of these keys, "people"
(pretend there were more, as there would be in the penultimate element of array
) is passed by its each
into the inner block, whose block variable, k
, is assigned the value "people"
. We then use Hash#update (aka merge!
) to merge the hash:
这些键中的第一个,“人”(假装有更多,因为将存在于数组的倒数第二个元素中)通过其每个传递到内部块中,其块变量k被赋予值“people”。然后我们使用Hash#update(aka merge!)来合并哈希:
{k=>h[k]} #=> {"people"=>'http://www.thing.com/this-post'}
into new_hash
, but only because:
进入new_hash,但仅仅是因为:
hash_keys.include?(k)
#=> ["people", "chairs", "footballs", "crayons", "cat_lives"].include?("people")
#=> true
evaluates to true
. Note that this will evaluate to false
for the key "balloons"
, so the hash in array
with that key will not be merged. update
's block:
评估为真。请注意,对于键“balloon”,这将评估为false,因此不会合并具有该键的数组中的散列。更新块:
{ |k,g,http| { qty: g[:qty], http: (g[:http] << http) } }
is crucial. This is update
's way of determining the value of a key that is in both new_hash
and in the hash being merged, {k=>h[k]}
. The three block variables are assigned the following values by update
:
至关重要。这是更新确定new_hash和正在合并的散列中的键值的方法,{k => h [k]}。通过更新为三个块变量分配以下值:
k : the key ("people")
g : the current value of `new_hash[k]`
#=> `new_hash["people"] => {:qty=>"50", :http=>[]}`
http: the value of the key/value being merged
#=> 'http://www.thing.com/this-post'
We want the merged hash value for key "people"
to be:
我们希望键“people”的合并哈希值为:
{ qty: g[:qty], http: (g[:http] << http) }
#=> { qty: 50, http: ([] << 'http://www.thing.com/this-post') }
#=> { qty: 50, http: ['http://www.thing.com/this-post'] }
so now:
现在:
new_hash
#=> {"people" =>{:qty=>"50", :http=>['http://www.thing.com/this-post']},
# "chairs" =>{:qty=>"23", :http=>[]},
# "footballs"=>{:qty=>"5", :http=>[]},
# "crayons" =>{:qty=>"1", :http=>[]},
# "cat_lives"=>{:qty=>"9", :http=>[]}}
We do the same for each of the other elements of array
.
我们对数组的每个其他元素都这样做。
Lastly, we need to return the merged new_hash
, so we make last line of the method:
最后,我们需要返回合并的new_hash,因此我们创建方法的最后一行:
new_hash
#2
2
What you have is a mix of hashes, arrays, and something in the middle. I'm going to assume the following inputs:
你所拥有的是混合的哈希,数组和中间的东西。我将假设以下输入:
categories = {'people'=>'50',
'chairs'=>'23',
'footballs'=>'5',
'crayons'=>'1'}
and:
和:
posts = [['people', 'http://www.thing.com/this-post'],
['footballs','http://www.thing.com/that-post'],
['people','http://www.thing.com/nice-post'],
['footballs','http://www.thing.com/other-post'],
['people','http://www.thing.com/thingy-post'],
['footballs','http://www.thing.com/the-post'],
['people','http://www.thing.com/the-post'],
['crayons','http://www.thing.com/the-blah'],
['chairs','http://www.thing.com/the-page']]
and the following output:
以及以下输出:
[['people', '50', ['http://www.thing.com/this-post', 'http://www.thing.com/nice-post', 'http://www.thing.com/thingy-post']],
[['footballs', '5', ['http://www.thing.com/the-post', 'http://www.thing.com/the-post']],
['crayons', '1', ['http://www.thing.com/the-blah']],
['chairs', '23' => {'chairs'=>'http://www.thing.com/the-page']]]
In which case what you would need is:
在这种情况下,您需要的是:
categories.map do |name, count|
[name, count, posts.select do |category, _|
category == name
end.map { |_, post| post }]
end
You need to understand the different syntax for Array and Hash in Ruby:
您需要了解Ruby中Array和Hash的不同语法:
Hash:
哈希:
{ 'key1' => 'value1',
'key2' => 'value2' }
Array:
阵:
[ 'item1', 'item2', 'item3', 'item4' ]
A Hash in ruby (like in every other language) can't have more than once instance of any single key, meaning that a Hash {'key1' => 1, 'key1' => 2}
is invalid and will result in an unexpected value (duplicate keys are overridden - you'll have {'key1' => 2 }
).
ruby中的哈希(与其他语言一样)不能有任何单个键的多次实例,这意味着哈希{'key1'=> 1,'key1'=> 2}无效并将导致意外的值(覆盖重复的键 - 你将{'key1'=> 2})。
#3
0
You could also do this
你也可以这样做
cat = [{'people'=>'50'},
{'chairs'=>'23'},
{'footballs'=>'5'},
{'crayons'=>'1'}]
pages = [{'people'=>'http://www.thing.com/this-post'},
{'footballs'=>'http://www.thing.com/that-post'},
{'people'=>'http://www.thing.com/nice-post'},
{'footballs'=>'http://www.thing.com/other-post'},
{'people'=>'http://www.thing.com/thingy-post'},
{'footballs'=>'http://www.thing.com/the-post'},
{'people'=>'http://www.thing.com/the-post'},
{'crayons'=>'http://www.thing.com/the-blah'},
{'chairs'=>'http://www.thing.com/the-page'}]
cat.map do |c|
c.merge(Hash['pages',pages.collect{|h| h[c.keys.pop]}.compact])
end
#=> [{"people"=>"50", "pages"=>["http://www.thing.com/this-post", "http://www.thing.com/nice-post", "http://www.thing.com/thingy-post", "http://www.thing.com/the-post"]},
{"chairs"=>"23", "pages"=>["http://www.thing.com/the-page"]},
{"footballs"=>"5", "pages"=>["http://www.thing.com/that-post", "http://www.thing.com/other-post", "http://www.thing.com/the-post"]},
{"crayons"=>"1", "pages"=>["http://www.thing.com/the-blah"]}]
Which is closer to your request but far less usable than some of the other posts.
哪个更接近您的请求,但远不如其他一些帖子可用。
#1
1
Since there is some confusion about the format of the data, I will suggest how you might effectively structure both the input and the output. I will first present some code you could use, then give an example of how it's used, then explain what is happening.
由于对数据格式存在一些混淆,我将建议您如何有效地构建输入和输出。我将首先介绍一些您可以使用的代码,然后举例说明它是如何使用的,然后解释发生了什么。
Code
码
def merge_em(hash, array)
hash_keys = hash.keys
new_hash = hash_keys.each_with_object({}) { |k,h|
h[k] = { qty: hash[k], http: [] } }
array.each do |h|
h.keys.each do |k|
(new_hash.update({k=>h[k]}) { |k,g,http|
{qty: g[:qty], http: (g[:http] << http)}}) if hash_keys.include?(k)
end
end
new_hash
end
Example
例
Here is a hash that I have modified to include a key/value pair that does not appear in the array below:
这是一个哈希,我已修改为包含未出现在下面数组中的键/值对:
hash = {'people' =>'50', 'chairs' =>'23', 'footballs'=>'5',
'crayons'=> '1', 'cat_lives'=> '9'}
Below is your array of hashes that is to be merged into hash
. You'll see I've added a key/value pair to your hash with key "chairs"
. As I hope to make clear, the code is no different (i.e., not simplified) if we know in advance that each hash has only one key value pair. (Aside: if, for example, we want want the key from a hash h
that is known to have only one key, we still have to pull out all the keys into an array and then take the only element of the array: h.keys.first
).
下面是要合并为哈希的哈希数组。你会看到我已经使用键“chair”为你的哈希添加了一个键/值对。正如我希望的那样,如果我们事先知道每个散列只有一个键值对,则代码没有区别(即,不简化)。 (旁白:例如,如果我们想要一个已知只有一个密钥的哈希h中的密钥,我们仍然必须将所有密钥拉出到一个数组中,然后取出该数组的唯一元素:h。 keys.first)。
I have also added a hash to the array that has no key that is among hash
's keys.
我还在数组中添加了一个哈希,该哈希没有哈希键中的键。
array =
[{'people' =>'http://www.thing.com/this-post'},
{'footballs'=>'http://www.thing.com/that-post'},
{'people' =>'http://www.thing.com/nice-post'},
{'footballs'=>'http://www.thing.com/other-post'},
{'people' =>'http://www.thing.com/thingy-post'},
{'footballs'=>'http://www.thing.com/the-post'},
{'people' =>'http://www.thing.com/the-post'},
{'crayons' =>'http://www.thing.com/the-blah'},
{'chairs' =>'http://www.thing.com/the-page',
'crayons' =>'http://www.thing.com/blah'},
{'balloons' =>'http://www.thing.com/the-page'}
]
We now merge the information from array
into hash
, and at the same time change the structure of hash
to something more suitable:
我们现在将数组中的信息合并到散列中,同时将散列结构更改为更合适的内容:
result = merge_em(hash, array)
#=> {"people" =>{:qty=>"50",
# :http=>["http://www.thing.com/this-post",
# "http://www.thing.com/nice-post",
# "http://www.thing.com/thingy-post",
# "http://www.thing.com/the-post"]},
# "chairs" =>{:qty=>"23",
# :http=>["http://www.thing.com/the-page"]},
# "footballs"=>{:qty=>"5",
# :http=>["http://www.thing.com/that-post",
# "http://www.thing.com/other-post",
# "http://www.thing.com/the-post"]},
# "crayons" =>{:qty=>"1",
# :http=>["http://www.thing.com/the-blah",
# "http://www.thing.com/blah"]},
# "cat_lives"=>{:qty=>"9",
# :http=>[]}}
I've assumed you want to look up the content of result
with hash
's keys. It is therefore convenient to make the values associated with those keys hashes themselves, with keys :qty
and http
. The former is for the values in hash
(the naming may be wrong); the latter is an array containing the strings drawn from array
.
我假设你想用哈希的键查找结果的内容。因此,使用键:qty和http使得与这些键相关联的值自身散列是很方便的。前者是哈希值(命名可能是错误的);后者是一个包含从数组中绘制的字符串的数组。
This way, if we want the value for the key "crayons"
, we could write:
这样,如果我们想要键“蜡笔”的值,我们可以写:
result["crayons"]
#=> {:qty=>"1",
# :http=>["http://www.thing.com/the-blah", "http://www.thing.com/blah"]}
or
要么
irb(main):133:0> result["crayons"][:qty]
#=> "1"
irb(main):134:0> result["crayons"][:http]
#=> ["http://www.thing.com/the-blah", "http://www.thing.com/blah"]
Explanation
说明
Let's go through this line-by-line. First, we need to reference hash.keys
more than once, so let's make it a variable:
让我们逐行完成。首先,我们需要多次引用hash.keys,所以让它变成一个变量:
hash_keys = hash.keys
#=> ["people", "chairs", "footballs", "crayons", "cat_lives"]
We may as well convert this hash to the output format now. We could do it during the merge operation below, but I think is clearer to do it as a separate step:
我们现在也可以将此哈希转换为输出格式。我们可以在下面的合并操作中执行此操作,但我认为将其作为单独的步骤更清楚:
new_hash = hash_keys.each_with_object({}) { |k,h|
h[k] = { qty: hash[k], http: [] } }
#=> {"people" =>{:qty=>"50", :http=>[]},
# "chairs" =>{:qty=>"23", :http=>[]},
# "footballs"=>{:qty=>"5", :http=>[]},
# "crayons" =>{:qty=>"1", :http=>[]},
# "cat_lives"=>{:qty=>"9", :http=>[]}}
Now we merge each (hash) element of array
into new_hash
:
现在我们将数组的每个(哈希)元素合并到new_hash中:
array.each do |h|
h.keys.each do |k|
(new_hash.update({k=>h[k]}) { |k,g,http|
{ qty: g[:qty], http: (g[:http] << http) } }) if hash_keys.include?(k)
end
end
The first hash h
from array
that is passed into the block by each
is:
每个传递给块的数组中的第一个哈希值是:
{'people'=>'http://www.thing.com/this-post'}
which is assigned to the block variable h
. We next construct an array of h
's keys:
分配给块变量h。接下来我们构造一个h键的数组:
h.keys #=> ["people"]
The first of these keys, "people"
(pretend there were more, as there would be in the penultimate element of array
) is passed by its each
into the inner block, whose block variable, k
, is assigned the value "people"
. We then use Hash#update (aka merge!
) to merge the hash:
这些键中的第一个,“人”(假装有更多,因为将存在于数组的倒数第二个元素中)通过其每个传递到内部块中,其块变量k被赋予值“people”。然后我们使用Hash#update(aka merge!)来合并哈希:
{k=>h[k]} #=> {"people"=>'http://www.thing.com/this-post'}
into new_hash
, but only because:
进入new_hash,但仅仅是因为:
hash_keys.include?(k)
#=> ["people", "chairs", "footballs", "crayons", "cat_lives"].include?("people")
#=> true
evaluates to true
. Note that this will evaluate to false
for the key "balloons"
, so the hash in array
with that key will not be merged. update
's block:
评估为真。请注意,对于键“balloon”,这将评估为false,因此不会合并具有该键的数组中的散列。更新块:
{ |k,g,http| { qty: g[:qty], http: (g[:http] << http) } }
is crucial. This is update
's way of determining the value of a key that is in both new_hash
and in the hash being merged, {k=>h[k]}
. The three block variables are assigned the following values by update
:
至关重要。这是更新确定new_hash和正在合并的散列中的键值的方法,{k => h [k]}。通过更新为三个块变量分配以下值:
k : the key ("people")
g : the current value of `new_hash[k]`
#=> `new_hash["people"] => {:qty=>"50", :http=>[]}`
http: the value of the key/value being merged
#=> 'http://www.thing.com/this-post'
We want the merged hash value for key "people"
to be:
我们希望键“people”的合并哈希值为:
{ qty: g[:qty], http: (g[:http] << http) }
#=> { qty: 50, http: ([] << 'http://www.thing.com/this-post') }
#=> { qty: 50, http: ['http://www.thing.com/this-post'] }
so now:
现在:
new_hash
#=> {"people" =>{:qty=>"50", :http=>['http://www.thing.com/this-post']},
# "chairs" =>{:qty=>"23", :http=>[]},
# "footballs"=>{:qty=>"5", :http=>[]},
# "crayons" =>{:qty=>"1", :http=>[]},
# "cat_lives"=>{:qty=>"9", :http=>[]}}
We do the same for each of the other elements of array
.
我们对数组的每个其他元素都这样做。
Lastly, we need to return the merged new_hash
, so we make last line of the method:
最后,我们需要返回合并的new_hash,因此我们创建方法的最后一行:
new_hash
#2
2
What you have is a mix of hashes, arrays, and something in the middle. I'm going to assume the following inputs:
你所拥有的是混合的哈希,数组和中间的东西。我将假设以下输入:
categories = {'people'=>'50',
'chairs'=>'23',
'footballs'=>'5',
'crayons'=>'1'}
and:
和:
posts = [['people', 'http://www.thing.com/this-post'],
['footballs','http://www.thing.com/that-post'],
['people','http://www.thing.com/nice-post'],
['footballs','http://www.thing.com/other-post'],
['people','http://www.thing.com/thingy-post'],
['footballs','http://www.thing.com/the-post'],
['people','http://www.thing.com/the-post'],
['crayons','http://www.thing.com/the-blah'],
['chairs','http://www.thing.com/the-page']]
and the following output:
以及以下输出:
[['people', '50', ['http://www.thing.com/this-post', 'http://www.thing.com/nice-post', 'http://www.thing.com/thingy-post']],
[['footballs', '5', ['http://www.thing.com/the-post', 'http://www.thing.com/the-post']],
['crayons', '1', ['http://www.thing.com/the-blah']],
['chairs', '23' => {'chairs'=>'http://www.thing.com/the-page']]]
In which case what you would need is:
在这种情况下,您需要的是:
categories.map do |name, count|
[name, count, posts.select do |category, _|
category == name
end.map { |_, post| post }]
end
You need to understand the different syntax for Array and Hash in Ruby:
您需要了解Ruby中Array和Hash的不同语法:
Hash:
哈希:
{ 'key1' => 'value1',
'key2' => 'value2' }
Array:
阵:
[ 'item1', 'item2', 'item3', 'item4' ]
A Hash in ruby (like in every other language) can't have more than once instance of any single key, meaning that a Hash {'key1' => 1, 'key1' => 2}
is invalid and will result in an unexpected value (duplicate keys are overridden - you'll have {'key1' => 2 }
).
ruby中的哈希(与其他语言一样)不能有任何单个键的多次实例,这意味着哈希{'key1'=> 1,'key1'=> 2}无效并将导致意外的值(覆盖重复的键 - 你将{'key1'=> 2})。
#3
0
You could also do this
你也可以这样做
cat = [{'people'=>'50'},
{'chairs'=>'23'},
{'footballs'=>'5'},
{'crayons'=>'1'}]
pages = [{'people'=>'http://www.thing.com/this-post'},
{'footballs'=>'http://www.thing.com/that-post'},
{'people'=>'http://www.thing.com/nice-post'},
{'footballs'=>'http://www.thing.com/other-post'},
{'people'=>'http://www.thing.com/thingy-post'},
{'footballs'=>'http://www.thing.com/the-post'},
{'people'=>'http://www.thing.com/the-post'},
{'crayons'=>'http://www.thing.com/the-blah'},
{'chairs'=>'http://www.thing.com/the-page'}]
cat.map do |c|
c.merge(Hash['pages',pages.collect{|h| h[c.keys.pop]}.compact])
end
#=> [{"people"=>"50", "pages"=>["http://www.thing.com/this-post", "http://www.thing.com/nice-post", "http://www.thing.com/thingy-post", "http://www.thing.com/the-post"]},
{"chairs"=>"23", "pages"=>["http://www.thing.com/the-page"]},
{"footballs"=>"5", "pages"=>["http://www.thing.com/that-post", "http://www.thing.com/other-post", "http://www.thing.com/the-post"]},
{"crayons"=>"1", "pages"=>["http://www.thing.com/the-blah"]}]
Which is closer to your request but far less usable than some of the other posts.
哪个更接近您的请求,但远不如其他一些帖子可用。