如何在Ruby中添加到现有哈希 [英] How to add to an existing hash in Ruby

查看:81
本文介绍了如何在Ruby中添加到现有哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于将key => value对添加到Ruby中现有的填充哈希中,我正在研究Apress的Beginning Ruby,并且刚刚完成了哈希章节.

In regards to adding an key => value pair to an existing populated hash in Ruby, I'm in the process of working through Apress' Beginning Ruby and have just finished the hashes chapter.

我正在尝试找到最简单的方法来实现散列来达到与数组相同的结果:

I am trying to find the simplest way to achieve the same results with hashes as this does with arrays:

x = [1, 2, 3, 4]
x << 5
p x

推荐答案

如果您有哈希,则可以通过以下键引用它们来向其中添加项目:

If you have a hash, you can add items to it by referencing them by key:

hash = { }
hash[:a] = 'a'
hash[:a]
# => 'a'

在这里,就像[ ]创建一个空数组一样,{ }将创建一个空哈希.

Here, like [ ] creates an empty array, { } will create a empty hash.

数组按特定顺序包含零个或多个元素,其中元素可以重复.散列具有零个或多个由按键组织的元素,其中键可能不会重复,但存储在这些位置的值可以重复.

Arrays have zero or more elements in a specific order, where elements may be duplicated. Hashes have zero or more elements organized by key, where keys may not be duplicated but the values stored in those positions can be.

Ruby中的哈希非常灵活,可以拥有几乎可以扔给它的任何类型的密钥.这使其与您在其他语言中找到的字典结构有所不同.

Hashes in Ruby are very flexible and can have keys of nearly any type you can throw at it. This makes it different from the dictionary structures you find in other languages.

请务必记住,哈希键的特定性质通常很重要:

It's important to keep in mind that the specific nature of a key of a hash often matters:

hash = { :a => 'a' }

# Fetch with Symbol :a finds the right value
hash[:a]
# => 'a'

# Fetch with the String 'a' finds nothing
hash['a']
# => nil

# Assignment with the key :b adds a new entry
hash[:b] = 'Bee'

# This is then available immediately
hash[:b]
# => "Bee"

# The hash now contains both keys
hash
# => { :a => 'a', :b => 'Bee' }

Ruby on Rails通过提供HashWithIndifferentAccess在某种程度上将其混淆,它将在Symbol和String寻址方法之间自由转换.

Ruby on Rails confuses this somewhat by providing HashWithIndifferentAccess where it will convert freely between Symbol and String methods of addressing.

您还可以索引几乎所有内容,包括类,数字或其他哈希.

You can also index on nearly anything, including classes, numbers, or other Hashes.

hash = { Object => true, Hash => false }

hash[Object]
# => true

hash[Hash]
# => false

hash[Array]
# => nil

哈希可以转换为数组,反之亦然:

Hashes can be converted to Arrays and vice-versa:

# Like many things, Hash supports .to_a
{ :a => 'a' }.to_a
# => [[:a, "a"]]

# Hash also has a handy Hash[] method to create new hashes from arrays
Hash[[[:a, "a"]]]
# => {:a=>"a"} 

在将事物插入"哈希中时,您可以一次执行一次,也可以使用merge方法组合哈希:

When it comes to "inserting" things into a Hash you may do it one at a time, or use the merge method to combine hashes:

{ :a => 'a' }.merge(:b => 'b')
# {:a=>'a',:b=>'b'}

请注意,这不会更改原始哈希,而是返回一个新哈希.如果要将一个散列合并到另一个散列中,可以使用merge!方法:

Note that this does not alter the original hash, but instead returns a new one. If you want to combine one hash into another, you can use the merge! method:

hash = { :a => 'a' }

# Returns the result of hash combined with a new hash, but does not alter
# the original hash.
hash.merge(:b => 'b')
# => {:a=>'a',:b=>'b'}

# Nothing has been altered in the original
hash
# => {:a=>'a'}

# Combine the two hashes and store the result in the original
hash.merge!(:b => 'b')
# => {:a=>'a',:b=>'b'}

# Hash has now been altered
hash
# => {:a=>'a',:b=>'b'}

与String和Array上的许多方法一样,!表示它是就地操作.

Like many methods on String and Array, the ! indicates that it is an in-place operation.

这篇关于如何在Ruby中添加到现有哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆