English 中文(简体)
2. 在废墟中使用相同的名称阵列
原标题:Merge same named arrays in ruby hashes

我要谈两点,如:

hash_1 = {:a=>{:b=>3, :c=>{:stuff1=>[{:d=>1, :e=>2}, {:d=>4, :e=>2}], :stuff2=>[{:f=>33, :g=>44}, {:f=>55, :g=>66}], :h=>4}}}

hash_2 = {:a=>{:b=>3, :c=>{:stuff1=>[{:d=>8, :e=>5}, {:d=>7, :e=>5}], :stuff2=>[{:f=>45, :g=>89}, {:f=>78, :g=>67}], :h=>4}}}

改为:

result = {:a=>{:b=>3, :c=>{:stuff1=>[{:d=>1, :e=>2}, {:d=>4, :e=>2}, {:d=>8, :e=>5}, {:d=>7, :e=>5}], :stuff2=>[{:f=>33, :g=>44}, {:f=>55, :g=>66}, {:f=>45, :g=>89}, {:f=>78, :g=>67}], :h=>4}}}

我发现,post,但我的案件充满了nes,因此,一些良好的垃圾手的任何帮助都将受到赞赏。

基本上,我想“合并”同名关键人物的阵列价值,。 对应这些钥匙的数值为arrays。 当然,replacecode>hash_1 s :stuff1 hash_2 s :stuff1 阵列(类似:stuff2),但我希望有一个阵列<>+>>>>>>>>>>>>>>>>>+>>>>>>>>>>>>>>>>>>> 合并类型,而不是更新/替换或合并! ......

hash_1.merge(hash_2)  # NOT what I want => {:a=>{:b=>3, :c=>{:stuff1=>[{:d=>8, :e=>5}, {:d=>7, :e=>5}], :stuff2=>[{:f=>45, :g=>89}, {:f=>78, :g=>67}], :h=>4}}}

I m 使用了1.9.2, btw。 我知道已经过迟,虽然我不认为这影响到答案。

感谢!

最佳回答
# adapted from http://snippets.dzone.com/posts/show/4706
class Hash
  def deep_merge_with_array_values_concatenated(hash)
    target = dup

    hash.keys.each do |key|
      if hash[key].is_a? Hash and self[key].is_a? Hash
        target[key] = target[key].deep_merge_with_array_values_concatenated(hash[key])
        next
      end

      if hash[key].is_a?(Array) && target[key].is_a?(Array)
        target[key] = target[key] + hash[key]
      else
        target[key] = hash[key]
      end
    end

    target
  end
end

p hash_1.deep_merge_with_array_values_concatenated(hash_2)
问题回答

您可以确定合并方法的组块,将要求每一组重复钥匙。

hash_1.merge(hash_2) do |key, old_value, new_value|
  old_value + new_value
end 

我认为这些规格并不完备。 任何方面,都采用功能性的复读方法(第二根基只用于混淆阵列价值):

class Hash
  def concat_on_common_array_values(hash)
    Hash[map do |key, value|
      if value.is_a?(Hash) && hash[key].is_a?(Hash)
        [key, value.concat_on_common_array_values(hash[key])] 
      elsif value.is_a?(Array) && hash[key].is_a?(Array)
        [key, value + hash[key]]
      else
        [key, value]
      end      
    end]
  end
end

p hash_1.concat_on_common_array_values(hash_2)
# {:a=>{:b=>3, :c=>{:stuff1=>[{:d=>1, :e=>2}, {:d=>4, :e=>2}, {:d=>8, :e=>5}, {:d=>7, :e=>5}], :stuff2=>[{:f=>33, :g=>44}, {:f=>55, :g=>66}, {:f=>45, :g=>89}, {:f=>78, :g=>67}], :h=>4}}}




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...