ruby hash添加数据_如何在Ruby中向Hash添加元素?
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:
程序:
=beginRuby 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.chompputs "Enter the value:" val = gets.chomphsh = 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:
程序:
=beginRuby 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添加數據
總結
以上是生活随笔為你收集整理的ruby hash添加数据_如何在Ruby中向Hash添加元素?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: stl swap函数_C ++ STL
- 下一篇: fedora操作系统优缺点_不同类型的操