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