如何更新循环内的 Ruby 嵌套哈希?

发布于 2024-11-06 12:06:56 字数 411 浏览 4 评论 0原文

我正在 ruby​​ rexml 中创建一个嵌套哈希,并希望在进入循环时更新哈希。

我的代码是这样的:

hash = {}
doc.elements.each(//address) do |n|
  a = # ... 
  b = # ...
  hash = { "NAME" => { a => { "ADDRESS" => b } } }
end

当我执行上面的代码时,哈希值被覆盖,我只得到循环最后一次迭代中的信息。

我不想使用以下方式,因为它使我的代码变得冗长

hash["NAME"] = {}
hash["NAME"][a] = {} 

等等...

所以有人可以帮助我如何完成这项工作...

I'm creating a nested hash in ruby rexml and want to update the hash when i enter a loop.

My code is like:

hash = {}
doc.elements.each(//address) do |n|
  a = # ... 
  b = # ...
  hash = { "NAME" => { a => { "ADDRESS" => b } } }
end

When I execute the above code the hash gets overwritten and I get only the info in the last iteration of the loop.

I don't want to use the following way as it makes my code verbose

hash["NAME"] = {}
hash["NAME"][a] = {} 

and so on...

So could someone help me out on how to make this work...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眸中客 2024-11-13 12:06:56

假设名称是唯一的:

hash.merge!({"NAME" => { a => { "ADDRESS" => b } } })

Assuming the names are unique:

hash.merge!({"NAME" => { a => { "ADDRESS" => b } } })
雨夜星沙 2024-11-13 12:06:56

您始终在每次迭代中创建一个新的哈希值,该哈希值保存在 hash 中。

只需直接在现有的哈希中分配密钥即可:

hash["NAME"] = { a => { "ADDRESS" => b } }

You always create a new hash in each iteration, which gets saved in hash.

Just assign the key directly in the existing hash:

hash["NAME"] = { a => { "ADDRESS" => b } }
冰雪梦之恋 2024-11-13 12:06:56
hash = {"NAME" => {}}

doc.elements.each('//address') do |n|
  a = ...
  b = ...
  hash['NAME'][a] = {'ADDRESS' => b, 'PLACE' => ...}
end
hash = {"NAME" => {}}

doc.elements.each('//address') do |n|
  a = ...
  b = ...
  hash['NAME'][a] = {'ADDRESS' => b, 'PLACE' => ...}
end
鸠魁 2024-11-13 12:06:56
blk = proc { |hash, key| hash[key] = Hash.new(&blk) }

hash = Hash.new(&blk)

doc.elements.each('//address').each do |n|
  a = # ...
  b = # ...
  hash["NAME"][a]["ADDRESS"] = b
end

基本上创建一个延迟实例化的无限重复的哈希值。

编辑:只是想到了一些可行的方法,这仅使用几个非常简单的哈希进行了测试,因此可能会出现一些问题。

class Hash
  def can_recursively_merge? other
    Hash === other
  end

  def recursive_merge! other
    other.each do |key, value|
      if self.include? key and self[key].can_recursively_merge? value
        self[key].recursive_merge! value
      else
        self[key] = value
      end
    end
    self
  end
end

然后使用 hash.recursive_merge! {“名称”=> { 一个 => {“地址”=> b } } } 在你的代码块中。

如果您在其上定义了 recursive_merge!can_recusively_merge? 方法,这将简单地递归合并哈希的层次结构以及任何其他类型。

blk = proc { |hash, key| hash[key] = Hash.new(&blk) }

hash = Hash.new(&blk)

doc.elements.each('//address').each do |n|
  a = # ...
  b = # ...
  hash["NAME"][a]["ADDRESS"] = b
end

Basically creates a lazily instantiated infinitely recurring hash of hashes.

EDIT: Just thought of something that could work, this is only tested with a couple of very simple hashes so may have some problems.

class Hash
  def can_recursively_merge? other
    Hash === other
  end

  def recursive_merge! other
    other.each do |key, value|
      if self.include? key and self[key].can_recursively_merge? value
        self[key].recursive_merge! value
      else
        self[key] = value
      end
    end
    self
  end
end

Then use hash.recursive_merge! { "NAME" => { a => { "ADDRESS" => b } } } in your code block.

This simply recursively merges a heirachy of hashes, and any other types if you define the recursive_merge! and can_recusively_merge? methods on them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文