将一个哈希值保存在另一个哈希值中是常见做法吗?

发布于 2024-07-23 12:39:04 字数 225 浏览 10 评论 0原文

我想将一些哈希对象保存到集合中(在 Java 世界中将其视为列表)。 我上网搜索Ruby中是否有类似的数据结构,但没有找到。 目前,我一直在尝试将哈希 a[] 保存到哈希 b[] 中,但在尝试从哈希 中获取数据时遇到了问题b[]

Ruby 上有内置的集合数据结构吗? 如果不是,将哈希值保存在另一个哈希值中是否是常见做法?

I'd like to save some hash objects to a collection (in the Java world think of it as a List). I search online to see if there is a similar data structure in Ruby and have found none. For the moment being I've been trying to save hash a[] into hash b[], but have been having issues trying to get data out of hash b[].

Are there any built-in collection data structures on Ruby? If not, is saving a hash in another hash common practice?

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

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

发布评论

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

评论(5

落花浅忆 2024-07-30 12:39:04

到目前为止,这里的所有答案都是关于哈希中的哈希,而不是哈希加哈希,因此出于完整性的原因,我将附注以下内容:

# Define two independent Hash objects
hash_a = { :a => 'apple', :b => 'bear', :c => 'camel' }
hash_b = { :c => 'car', :d => 'dolphin' }

# Combine two hashes with the Hash#merge method
hash_c = hash_a.merge(hash_b)

# The combined hash has all the keys from both sets
puts hash_c[:a] # => 'apple'
puts hash_c[:c] # => 'car', not 'camel' since B overwrites A

请注意,当您将 B 合并到 A 时,A 拥有的 B 中的任何键都是被覆盖。

All the answers here so far are about Hash in Hash, not Hash plus Hash, so for reasons of completeness, I'll chime in with this:

# Define two independent Hash objects
hash_a = { :a => 'apple', :b => 'bear', :c => 'camel' }
hash_b = { :c => 'car', :d => 'dolphin' }

# Combine two hashes with the Hash#merge method
hash_c = hash_a.merge(hash_b)

# The combined hash has all the keys from both sets
puts hash_c[:a] # => 'apple'
puts hash_c[:c] # => 'car', not 'camel' since B overwrites A

Note that when you merge B into A, any keys that A had that are in B are overwritten.

原谅我要高飞 2024-07-30 12:39:04

Ruby 中的列表是数组。 您可以使用 Hash.to_a

如果您尝试将哈希 a 与哈希 b 组合,可以使用 Hash .merge

编辑:如果您尝试将哈希 a 插入哈希 b,您可以这样做

b["Hash a"] = a;

Lists in Ruby are arrays. You can use Hash.to_a.

If you are trying to combine hash a with hash b, you can use Hash.merge

EDIT: If you are trying to insert hash a into hash b, you can do

b["Hash a"] = a;
瞄了个咪的 2024-07-30 12:39:04

如果是访问哈希中的哈希是问题所在,请尝试:

>> p = {:name => "Jonas", :pos => {:x=>100.23, :y=>40.04}}
=> {:pos=>{:y=>40.04, :x=>100.23}, :name=>"Jonas"}
>> p[:pos][:x]
=> 100.23

If it's accessing the hash in the hash that is the problem then try:

>> p = {:name => "Jonas", :pos => {:x=>100.23, :y=>40.04}}
=> {:pos=>{:y=>40.04, :x=>100.23}, :name=>"Jonas"}
>> p[:pos][:x]
=> 100.23
青萝楚歌 2024-07-30 12:39:04

这应该没有什么问题。

a = {:color => 'red', :thickness => 'not very'}
b = {:data => a, :reason => 'NA'}

也许您可以解释一下您遇到的问题。

There shouldn't be any problem with that.

a = {:color => 'red', :thickness => 'not very'}
b = {:data => a, :reason => 'NA'}

Perhaps you could explain what problems you're encountering.

尘曦 2024-07-30 12:39:04

这个问题并不完全清楚,但我认为你想要一个哈希列表(数组),对吧?

在这种情况下,您可以将它们放入一个数组中,这就像 Java 中的列表:

a = {:a => 1, :b => 2}
b = {:c => 3, :d => 4}
list = [a, b]

您可以检索像 list[0] 和 list[1] 这样的哈希值

The question is not completely clear, but I think you want to have a list (array) of hashes, right?

In that case, you can just put them in one array, which is like a list in Java:

a = {:a => 1, :b => 2}
b = {:c => 3, :d => 4}
list = [a, b]

You can retrieve those hashes like list[0] and list[1]

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