ruby hash添加数据
Before going through the ways to add elements to the hash instances, let us understand what could be called as a hash element. So, Hash is the collection of keys and their values. For example,
在介绍向哈希实例添加元素的方法之前,让我们了解什么可以称为哈希元素。 因此, 哈希是键及其值的集合。 例如,
Hash1 = {"Color" => "Red"} # is a hash object as it has a key value pair.
Now, let us understand the different ways through which we can add elements in the hash object.
现在,让我们了解在哈希对象中添加元素的不同方法。
Method 1: Use Hash.store() method
方法1:使用Hash.store()方法
This method is a public instance method that is defined in the ruby library especially for the Hash class. This method works in a way that it stores or assigns the given value into the key with which the method has been invoked. This method takes two parameters, one is the key and another one is the value of that particular key.
此方法是在ruby库中定义的公共实例方法,尤其是针对Hash类。 此方法的工作方式是,将给定值存储或分配给调用该方法的键。 此方法有两个参数,一个是键,另一个是该特定键的值。
This method does bring change in the actual hash because this method belongs to the category of destructive methods.
由于此方法属于破坏性方法的类别,因此该方法的确会带来实际哈希值的变化。
Syntax:
句法:
Hash_object.store(key,value)
Parameter(s) required:
所需参数:
This method takes two parameters, one is the key and another one is the value of that particular key.
此方法有两个参数,一个是键,另一个是该特定键的值。
Program:
程序:
=begin
Ruby program to demonstrate store method
=end
hash1={"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}
puts "Hash store implementation"
puts "Enter the key:"
ky = gets.chomp
puts "Enter the value:"
val = gets.chomp
hsh = hash1.store(ky,val)
puts "Key updated is #{hsh}"
puts "Self hash object : #{hash1}"
Output
输出量
Hash store implementation
Enter the key:
country
Enter the value:
India
Key updated is India
Self hash object : {"color"=>"Black", "object"=>"car", "love"=>"friends",
"fruit"=>"Kiwi", "vege"=>"potato", "country"=>"India"}
Explanation:
说明:
In the above code, you can observe that we are storing values in the hash object with the help of the Hash.store() method. You can see how we can update the value of a particular key in the hash object with the help of this method. This method is creating changes in the actual hash object because this method is one of the examples of destructive methods.
在上面的代码中,您可以观察到我们借助于Hash.store()方法将值存储在哈希对象中。 您将看到如何使用此方法来更新哈希对象中特定键的值。 因为此方法是破坏性方法的示例之一,所以该方法正在实际的哈希对象中创建更改。
Method 2: With the help of Hash.merge() method
方法2:借助于Hash.merge()方法
You can add elements in a particular hash with the help of another hash as well. You only have to store elements that you want to add in a different hash and apply merge operation.
您也可以在另一个哈希中添加元素。 您只需要将要添加的元素存储在其他哈希中,然后应用合并操作即可。
This method is a Public instance method and belongs to the Hash class which lives inside the library of Ruby language. This method works in a way that it returns a new hash object which contains the keys and values of self hash as well as another hash. If both the hashes are containing the same keys and values then the new hash will not contain duplicate keys and values or you can say that each key and value will be stored only for once. This method is one of the examples of non-destructive methods where the changes created by the methods are temporary or non-permanent.
此方法是Public实例方法,属于Hash类,它位于Ruby语言库中。 此方法的工作方式是返回一个新的哈希对象,该对象包含自身哈希的键和值以及另一个哈希。 如果两个哈希都包含相同的键和值,则新的哈希将不包含重复的键和值,或者可以说每个键和值仅存储一次。 此方法是非破坏性方法的示例之一,其中方法所产生的更改是临时的或非永久的。
Syntax:
句法:
Hash_object.merge(other_hash)
Parameter(s) required:
所需参数:
This method only takes one parameter and that argument is nothing but another hash instance you want to merge.
此方法仅使用一个参数,该参数不过是您要合并的另一个哈希实例。
Program:
程序:
=begin
Ruby program to demonstrate
Hash.merge(other_hash) method
=end
hsh={"colors"=>"red","letters"=>"a","Fruit"=>"Grapes","anything"=>"red","sweet"=>"ladoo"}
hsh1={"home"=>"shivaliknagar","city"=>"Haridwar","state"=>"Uttrakhand"}
puts "Hash.merge implementation:"
hash3 = hsh.merge(hsh1)
puts "The keys present in the new hash are: #{hash3}"
puts "Original hash : #{hsh}"
Output
输出量
Hash.merge implementation:
The keys present in the new hash are: {"colors"=>"red", "letters"=>"a",
"Fruit"=>"Grapes", "anything"=>"red", "sweet"=>"ladoo", "home"=>"shivaliknagar",
"city"=>"Haridwar", "state"=>"Uttrakhand"}
Original hash : {"colors"=>"red", "letters"=>"a", "Fruit"=>"Grapes",
"anything"=>"red", "sweet"=>"ladoo"}
Explanation:
说明:
In the above code, you can observe that you can merge with another hash with the help of the Hash.merge() method. You can see that the new hash is containing the keys and values of both the hashes. This method is not creating any changes in the original hash because this method is an example of non-destructive methods where the changes created by the method are non-permanent.
在上面的代码中,您可以观察到可以借助Hash.merge()方法与另一个哈希合并。 您可以看到新的哈希包含两个哈希的键和值。 此方法不会在原始哈希中创建任何更改,因为此方法是非破坏性方法的一个示例,该方法所创建的更改是非永久性的。
翻译自: https://www.includehelp.com/ruby/how-to-add-elements-to-a-hash-in-ruby.aspx
ruby hash添加数据