as_hash ruby
Hash.merge(other_hash)方法 (Hash.merge(other_hash) Method)
In this article, we will study about Hash.merge(other_hash) Method. The working of the method can’t be assumed because it’s quite a different name. Let us read its definition and understand its implementation with the help of syntax and program codes.
在本文中,我們將研究Hash.merge(other_hash)方法 。 無法假定該方法的工作原理,因為它的名稱完全不同。 讓我們閱讀其定義并在語法和程序代碼的幫助下了解其實現。
Method description:
方法說明:
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)
Argument(s) required:
所需參數:
This method only takes one parameter and that argument is nothing but another hash instance you want to merge.
此方法僅使用一個參數,該參數不過是您要合并的另一個哈希實例。
Example 1:
范例1:
=begin
Ruby program to demonstrate Hash.merge(other_hash) method
=end
hsh = {"colors" => "red","letters" => "a", "Fruit" => "Grapes", "anything"=>"red","sweet"=>"ladoo"}
hsh1 = {"home" => "shivalik nagar", "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"=>"shivalik nagar", "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 nonpermanent.
在上面的代碼中,您可以觀察到可以借助Hash.merge()方法與另一個哈希合并。 您可以看到新的哈希包含兩個哈希的鍵和值。 此方法不會在原始哈希中創建任何更改,因為此方法是非破壞性方法的示例,其中該方法創建的更改是永久的。
Example 2:
范例2:
=begin
Ruby program to demonstrate
Hash.merge(other_hash) method
=end
hsh = {"home" => "shivalik nagar", "city"=>"Haridwar","state"=>"Uttrakhand"}
hsh1 = {"home" => "shivalik nagar", "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: {"home"=>"shivalik nagar", "city"=>"Haridwar", "state"=>"Uttrakhand"}
Original hash : {"home"=>"shivalik nagar", "city"=>"Haridwar", "state"=>"Uttrakhand"}
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 no- duplicate keys and values even though there is a repetition of keys in both the hashes but the new hash is storing them only for once. 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 nonpermanent.
在上面的代碼中,您可以觀察到可以借助Hash.merge()方法與另一個哈希合并。 您可以看到,即使兩個哈希中都有鍵的重復,新哈希也包含不重復的鍵和值,但是新哈希僅將它們存儲一次。 此方法不會在原始哈希中創建任何更改,因為此方法是非破壞性方法的示例,其中該方法創建的更改是永久的。
翻譯自: https://www.includehelp.com/ruby/hash-merge-other_hash-method-with-example.aspx
as_hash ruby