如何添加到Ruby中的现有哈希(How to add to an existing hash in Ruby)

编程入门 行业动态 更新时间:2024-10-25 12:21:13
如何添加到Ruby中的现有哈希(How to add to an existing hash in Ruby)

关于在Ruby中为现有的填充哈希添加一个key => value对,我正在通过Apress的“Beginning Ruby”开始工作,刚刚完成了hashes章节。

我试图找到最简单的方式来实现与哈希相同的结果,这与数组:

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

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

最满意答案

如果你有一个散列,可以通过引用它们来添加项:

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

在这里,像[ ]创建一个空数组, { }将创建一个空的哈希。

数组具有特定顺序的零个或多个元素,其中元素可能被复制。 哈希具有零个或多个由键组织的元素,其中键可能不被重复,但存储在这些位置中的值可以是。

Ruby中的哈希非常灵活,可以具有几乎可以抛出的任何类型的键。 这使得它与您在其他语言中找到的字典结构不同。

重要的是要记住哈希密钥的具体性质通常很重要:

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寻址方法之间自由转换。

您也可以索引几乎任何东西,包括类,数字或其他哈希。

hash = { Object => true, Hash => false } hash[Object] # => true hash[Hash] # => false hash[Array] # => nil

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

# 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方法来组合散列:

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

请注意,这不会改变原始哈希,而是返回一个新的哈希。 如果要将一个哈希值组合成另一个,可以使用merge! 方法:

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上的很多方法一样! 表示它是一个就地操作。

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.

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 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"}

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'}

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'}

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

更多推荐

本文发布于:2023-08-07 15:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464890.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:add   Ruby   hash   existing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!