ruby 如何将新项目添加到哈希

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9571768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:51:53  来源:igfitidea点击:

How to add new item to hash

rubyhash

提问by Иван Бишевац

I'm new to Ruby and don't know how to add new item to already existing hash. For example, first I construct hash:

我是 Ruby 新手,不知道如何向现有哈希添加新项目。例如,首先我构造哈希:

hash = {item1: 1}

after that a want to add item2 so after this I have hash like this:

之后想要添加 item2 所以在这之后我有这样的哈希:

{item1: 1, item2: 2}

I don't know what method to do on hash, could someone help me?

我不知道如何处理哈希,有人可以帮助我吗?

回答by pjumble

Create the hash:

创建哈希:

hash = {:item1 => 1}

Add a new item to it:

向其添加一个新项目:

hash[:item2] = 2

回答by Alexander

If you want to add new items from another hash - use mergemethod:

如果要从另一个哈希添加新项目 - 使用merge方法:

hash = {:item1 => 1}
another_hash = {:item2 => 2, :item3 => 3}
hash.merge(another_hash) # {:item1=>1, :item2=>2, :item3=>3}

In your specific case it could be:

在您的特定情况下,它可能是:

hash = {:item1 => 1}
hash.merge({:item2 => 2}) # {:item1=>1, :item2=>2}

but it's not wise to use it when you should to add just one element more.

但是当您应该再添加一个元素时使用它是不明智的。

Pay attention that mergewill replace the values with the existing keys:

请注意,merge将使用现有键替换值:

hash = {:item1 => 1}
hash.merge({:item1 => 2}) # {:item1=>2}

exactly like hash[:item1] = 2

一模一样 hash[:item1] = 2

Also you should pay attention that mergemethod (of course) doesn't effect the original value of hash variable - it returns a new merged hash. If you want to replace the value of the hash variable then use merge!instead:

您还应该注意该merge方法(当然)不会影响散列变量的原始值 - 它返回一个新的合并散列。如果要替换散列变量的值,请merge!改用:

hash = {:item1 => 1}
hash.merge!({:item2 => 2})
# now hash == {:item1=>1, :item2=>2}

回答by shilovk

hash.store(key, value)- Stores a key-value pair in hash.

hash.store(key, value)- 在哈希中存储键值对。

Example:

例子:

hash   #=> {"a"=>9, "b"=>200, "c"=>4}
hash.store("d", 42) #=> 42
hash   #=> {"a"=>9, "b"=>200, "c"=>4, "d"=>42}

Documentation

Documentation

回答by Niklas B.

It's as simple as:

这很简单:

irb(main):001:0> hash = {:item1 => 1}
=> {:item1=>1}
irb(main):002:0> hash[:item2] = 2
=> 2
irb(main):003:0> hash
=> {:item1=>1, :item2=>2}

回答by Connor Leech

hash[key]=value Associates the value given by value with the key given by key.

hash[key]=value 将 value 给定的值与 key 给定的键相关联。

hash[:newKey] = "newValue"

From Ruby documentation: http://www.tutorialspoint.com/ruby/ruby_hashes.htm

来自 Ruby 文档:http: //www.tutorialspoint.com/ruby/ruby_hashes.htm

回答by KrisT

hash_items = {:item => 1}
puts hash_items 
#hash_items will give you {:item => 1}

hash_items.merge!({:item => 2})
puts hash_items 
#hash_items will give you {:item => 1, :item => 2}

hash_items.merge({:item => 2})
puts hash_items 
#hash_items will give you {:item => 1, :item => 2}, but the original variable will be the same old one. 

回答by Ravi Kashyap

Create hash as:

创建哈希为:

h = Hash.new
=> {}

Now insert into hash as:

现在插入哈希为:

h = Hash["one" => 1]