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

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541727.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541727.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541727.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

linux 安裝nfs 客戶端,在CentOS 7上安裝NFS服務器和客戶端

NFS服務器和客戶端安裝在CentOS 7上版本1.0作者:Srijan Kishore 在Twitter上關注howtoing最后編輯 16 / Dec / 2014本指南介紹如何在CentOS 7.0中配置NFS服務器網絡文件系統(NFS)是一種流行的分布式文件系統協議,可讓用戶在其服務器上安裝遠程目錄。 該系…

安裝ORACLE 時報錯 /jre/1.4.2/lib/i386/libawt.so:

最近在linux下安裝oracle 10g時,碰到如下問題: /tmp/OraInstall2011-09-11_02-16-11PM/jre/1.4.2/lib/i386/libawt.so: libXp.so.6: cannot open shared object file: No such file or directory occurred.. 網上找了下,真讓人費解呀&am…

Java線程start()vs run()方法及示例

Java | 線程start()vs run()方法 (Java | Thread start() vs run() Methods) When we call the start() method, it leads to the creation of a new thread. Then, it automatically calls the run() method. If we directly call the run() method, then no new thread will …

linux安裝卸載mysql,Linux6 系列 安裝、卸載mysql

Linux6 系列 安裝、卸載mysqlLinux6 系列 安裝、卸載mysqlLinux環境下載mysql:https://blog.csdn.net/weixin_40816738/article/details/90111456一、安裝環境依賴:yum install -y cmake make gcc gcc-c libaio ncurses ncurses-devel二、安裝流程1、軟件…

Python | 如何使用pip升級所有Python軟件包?

While using Python as a programming language, its a very common scenario to use a virtual environment and PIP, a package manager for python. 當使用Python作為編程語言時,使用虛擬環境和PIP (Python的程序包管理器)是一種非常常見的情況。 Its a common …

linux下enum類型占幾個字節,enum大小問題

問題描述板卡有兩個CPU,ARMMIPS,同時運行三個系統REE(linux) TEE(SierraTEE) SEE(TDS)。TEE跟SEE通過RPC進行通信,有enum成員的結構體信息傳遞會出錯,如下結構體:struct sTag {enum A;enum B;int C;enum D;};問題分析…

ASP.NET導出word實例

ASP.NET導出word實例 最近遇到一個題目就是如何在asp.net中將數據導出到word中,由于數據是動態的,所以需要在后臺拼出想要的的格式,翻遍了網頁找出了一個比較滿意的代碼,感謝那位高手。代碼如下: public void Download…

Java LocalDate類| toString()方法與示例

LocalDate類toString()方法 (LocalDate Class toString() method) toString() method is available in java.time package. toString()方法在java.time包中可用。 toString() method is used to represent this LocalDate as a String by using the standards ISO-8601 format.…

linux14.04 Apache,Ubuntu 14.04編譯安裝Apache

Ubuntu下編譯安裝apache需要預先編譯安裝多個依賴件,包括:apr, apr-util,pcre,zlib-devel,等,相當麻煩,記錄于此備查.由于Ubuntu系統默認安裝時沒有安裝C,所以也需要先安裝c編譯需要相關的組件。[注]apt-ca…

Android Jenkins自動化構建之路

install Jenkins 添加Jenkins的源(repository): sudo wget -O /etc/yum.repos.d/jenkins.repo http://jenkins-ci.org/redhat/jenkins.repo sudo rpm --import http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key復制代碼yum install Jenkins復制代碼…

java 根據類名示例化類_Java即時類| plusMillis()方法與示例

java 根據類名示例化類即時類plusMillis()方法 (Instant Class plusMillis() method) plusMillis() method is available in java.time package. plusMillis()方法在java.time包中可用。 plusMillis() method is used to add the given duration in milliseconds to this Insta…

linux dd入門,Linux基礎知識:Linux中DD命令詳解

1.dd命令簡介功能:把指定的輸入文件拷貝到指定的輸出文件中,并且在拷貝過程中可以進行格式轉換。可以用該命令實現DOS下的diskcopy命令的作用。先用dd命令把軟盤上的數據寫成硬盤的一個寄存文件,再把這個寄存文件寫入第二張軟盤上&#xff0c…

CSS 字體(font)實例

1、設置文本字體 font-family:"Times New Roman",Georgia,Serif font-family:Arial,Verdana,Sans-serif 2、設置字體尺寸 font-size: 100% 3、設置字體風格 font-style:normal font-style:italic font-style:oblique 4、設置字體的異體 font-variant:normal text-var…

Java Duration類| 帶示例的compareTo()方法

持續時間類compareTo()方法 (Duration Class compareTo() method) compareTo() method is available in java.time package. compareTo()方法在java.time包中可用。 compareTo() method is used to compare this Duration object to the given object. compareTo()方法用于將此…

linux定時任務執行url,科技常識:linux定時任務訪問url實例

今天小編跟大家講解下有關linux定時任務訪問url實例 ,相信小伙伴們對這個話題應該也很關注吧,小編也收集到了有關linux定時任務訪問url實例 的相關資料,希望小伙伴會喜歡也能夠幫助大家。這次linux定時任務設置成功,也算是自己學習…

lcase和ucase_在SQL中使用UCASE(),LCASE()和MID()函數

lcase和ucaseUpper Case, Lower Case and MID functions are scalar functions which return a single value, based in the input value. 大寫,小寫和MID函數是標量函數,它們基于輸入值返回單個值。 As you all know sometimes different databases ha…

Maven的Settings.xml配置文件解釋

該配置用于單用戶配置和全局配置, 單用戶配置默認存放于 ${user.home}/.m2/目錄中. 全局配置默認存放于Maven安裝目錄下面的conf目錄中. 這兩個默認的位置都可以修改. <?xml version"1.0" encoding"UTF-8"?> <settings xmlns"http://m…

linux ntp手動授時,關于我校NTP授時服務的使用說明

校園網用戶&#xff1a;我中心于近期采購了GPS北斗授時服務設備&#xff0c;該設備可實現純GPS模式、純北斗模式和混合模式與衛星對時&#xff0c;同時實現對校內設備授時的功能。支持所有NTP協議的服務器、PC、嵌入式設備等&#xff0c;包括但不限于&#xff1a;Microsoft Win…

一串字符串轉換為ascii_將ASCII字符串(char [])轉換為C中的BYTE數組

一串字符串轉換為asciiGiven an ASCII string (char[]) and we have to convert it into BYTE array (BYTE[]) in C. 給定一個ASCII字符串(char [])&#xff0c;我們必須將其轉換為C語言中的BYTE數組(BYTE [])。 Logic: 邏輯&#xff1a; To convert an ASCII string to BYTE…

debugging Auto Layout:Logical Errors

Logical Errors邏輯錯誤 Logical errors are simply bugs. Somewhere, you have an assumption that is faulty. Perhaps it’s an assumption about how Auto Layout calculates the views’ frames. Perhaps it’s an assumption about the set of constraints that you’ve …