日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

as_hash ruby_Hash.merge(other_hash)方法与Ruby中的示例

發(fā)布時間:2025/3/11 编程问答 58 豆豆
生活随笔 收集整理的這篇文章主要介紹了 as_hash ruby_Hash.merge(other_hash)方法与Ruby中的示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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)方法 。 無法假定該方法的工作原理,因為它的名稱完全不同。 讓我們閱讀其定義并在語法和程序代碼的幫助下了解其實現(xiàn)。

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語言庫中。 此方法的工作方式是返回一個新的哈希對象,該對象包含自身哈希的鍵和值以及另一個哈希。 如果兩個哈希都包含相同的鍵和值,則新的哈希將不包含重復的鍵和值,或者可以說每個鍵和值僅存儲一次。 此方法是非破壞性方法的示例之一,其中方法所產(chǎn)生的更改是臨時的或非永久的。

Syntax:

句法:

Hash_object.merge(other_hash)

Argument(s) required:

所需參數(shù):

This method only takes one parameter and that argument is nothing but another hash instance you want to merge.

此方法僅使用一個參數(shù),該參數(shù)不過是您要合并的另一個哈希實例。

Example 1:

范例1:

=beginRuby 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()方法與另一個哈希合并。 您可以看到新的哈希包含兩個哈希的鍵和值。 此方法不會在原始哈希中創(chuàng)建任何更改,因為此方法是非破壞性方法的示例,其中該方法創(chuàng)建的更改是永久的。

Example 2:

范例2:

=beginRuby 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()方法與另一個哈希合并。 您可以看到,即使兩個哈希中都有鍵的重復,新哈希也包含不重復的鍵和值,但是新哈希僅將它們存儲一次。 此方法不會在原始哈希中創(chuàng)建任何更改,因為此方法是非破壞性方法的示例,其中該方法創(chuàng)建的更改是永久的。

翻譯自: https://www.includehelp.com/ruby/hash-merge-other_hash-method-with-example.aspx

as_hash ruby

總結(jié)

以上是生活随笔為你收集整理的as_hash ruby_Hash.merge(other_hash)方法与Ruby中的示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。