如何向散列中添加新项?

时间:2021-03-23 07:33:13

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:

之后a想要添加item2之后我有这样的散列:

{:item1 => 1, :item2 =>2}

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

我不知道怎样处理哈希,有人能帮我吗?

7 个解决方案

#1


238  

Create the hash:

创建散列:

hash = {:item1 => 1}

Add a new item to it:

增加一个新项目:

hash[:item2] = 2

#2


58  

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

如果您想从另一个散列中添加新项,请使用合并方法:

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 merge will replace the values with the existing keys:

注意,合并将用现有的键替换值:

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

exactly like hash[:item1] = 2

就像散列[:item1] = 2

Also you should pay attention that merge method (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}

#3


25  

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}

#4


15  

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

散列。存储(键,值)-将键值对存储在散列中。

Example:

例子:

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

Documentation

文档

#5


11  

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

#6


2  

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. 

#7


1  

Create hash as:

创建散列:

h = Hash.new
=> {}

Now insert into hash as:

现在插入散列为:

h = Hash["one" => 1]

#1


238  

Create the hash:

创建散列:

hash = {:item1 => 1}

Add a new item to it:

增加一个新项目:

hash[:item2] = 2

#2


58  

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

如果您想从另一个散列中添加新项,请使用合并方法:

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 merge will replace the values with the existing keys:

注意,合并将用现有的键替换值:

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

exactly like hash[:item1] = 2

就像散列[:item1] = 2

Also you should pay attention that merge method (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}

#3


25  

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}

#4


15  

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

散列。存储(键,值)-将键值对存储在散列中。

Example:

例子:

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

Documentation

文档

#5


11  

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

#6


2  

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. 

#7


1  

Create hash as:

创建散列:

h = Hash.new
=> {}

Now insert into hash as:

现在插入散列为:

h = Hash["one" => 1]