as_hash ruby_Ruby中帶有示例的Hash.each_pair方法

as_hash ruby

Hash.each_pair方法 (Hash.each_pair Method)

In this article, we will study about Hash.each_pair Method. The working of this method can be predicted with the help of its name but it is not as simple as it seems. Well, we will understand this method with the help of its syntax and program code in the rest of the content.

在本文中,我們將研究Hash.each_pair方法 。 可以借助其名稱來預測此方法的工作,但是它并不像看起來那樣簡單。 好了,我們將在其余內容中借助其語法和程序代碼來理解此方法。

Method description:

方法說明:

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 invokes the block at least once for every individual key present in the hash object. The key-value pairs present in the hash object are passed as parameters. If you are not providing any block then you should expect an enumerator as the returned value from the each_pair method.

此方法是在ruby庫中定義的公共實例方法,尤其是針對Hash類。 此方法的工作方式是,針對哈希對象中存在的每個單個鍵至少調用一次該塊。 哈希對象中存在的鍵值對作為參數傳遞。 如果不提供任何塊,則應期望枚舉數作為each_pair方法的返回值。

Syntax:

句法:

    Hash_object.each_pair{|key,value| block}

Argument(s) required:

所需參數:

This method does not accept any arguments. However, you can pass a block along with this method.

此方法不接受任何參數。 但是,您可以通過此方法傳遞一個塊。

Example 1:

范例1:

=begin
Ruby program to demonstrate each_pair method
=end
hsh={"name"=>"Zorawar","class"=>"ukg","school"=>"AASSC","place"=>"Haridwar"}
puts "Hash each_pair implementation"
str = hsh.each_pair{|key,value| puts "#{key} is #{value}"}
puts str

Output

輸出量

Hash each_pair implementation
name is Zorawar
class is ukg
school is AASSC
place is Haridwar
{"name"=>"Zorawar", "class"=>"ukg", "school"=>"AASSC", "place"=>"Haridwar"}

Explanation:

說明:

In the above code, you may observe that we are printing every key-value pair from the hash object with the help of the Hash.each_pair method. The method is invoking a block, which is taking the key value as the argument from the hash object. This method is traversing through the hash object, processing them and giving us the desired output or you can say that letting us know about the value stored in a particular key.

在上面的代碼中,您可能會看到,借助于Hash.each_pair方法 ,我們正在從哈希對象中打印每個鍵值對。 該方法正在調用一個塊,該塊將鍵值作為哈希對象的參數。 該方法遍歷哈希對象,對其進行處理并為我們提供所需的輸出,或者您可以說讓我們知道存儲在特定鍵中的值。

Example 2:

范例2:

=begin
Ruby program to demonstrate each_pair method
=end
hsh={"name"=>"Zorawar","class"=>"ukg","school"=>"AASSC","place"=>"Haridwar"}
puts "Hash each_pair implementation"
str = hsh.each_pair
puts str

Output

輸出量

Hash each_pair implementation
#<Enumerator:0x0000560701eb6820>

Explanation:

說明:

In the above code, you can observe that when we are invoking the method without the block then we are getting an enumerator returned from the method.

在上面的代碼中,您可以觀察到,當我們在不使用該塊的情況下調用該方法時,就會得到該方法返回的枚舉數。

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

as_hash ruby

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

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

相關文章

mysql楊輝三角_兩個經典的小例子:楊輝三角和水仙花

package fllower;/*** 打印楊輝三角* author acer**/public class YangHui {public static void main(String[] args) {int[][] array new int[10][];//輸出十行for(int i 0;i<10;i){//行是從0開始 列數加1array[i] new int[i1];//從第一行開始&#xff0c;依次給每行加不…

《Effective Modern C++》翻譯--條款4:了解怎樣查看推導出的類型

條款4&#xff1a;了解怎樣查看推導出的類型 那些想要了解編譯器怎樣推導出的類型的人通常分為兩個陣營。第一種陣營是實用主義者。他們的動力通常來自于編敲代碼過程中(比如他們還在調試解決中)&#xff0c;他們利用編譯器進行尋找&#xff0c;并相信這個能幫他們找到問題的根…

julia 數組類型轉換_在Julia中確定類型的超類型

julia 數組類型轉換To determine the supertype of a type – we use the supertype() function, it accepts a data type and returns the concrete supertype of the given type. 要確定類型的超類型 –我們使用supertype()函數 &#xff0c;該函數接受數據類型并返回給定類型…

【js】JavaScript parser實現淺析

最近筆者的團隊遷移了webpack2&#xff0c;在遷移過程中&#xff0c;筆者發現webpack2中有相當多的兼容代碼&#xff0c;雖然外界有很多聲音一直在質疑作者為什么要破壞性更新&#xff0c;其實大家也都知道webpack1那種過于“靈活”的配置方式是有待商榷的&#xff0c;所以作者…

圖形學 射線相交算法_計算機圖形學中的陰極射線管(CRT)

圖形學 射線相交算法什么是陰極射線管(CRT)&#xff1f; (What is Cathode Ray Tube (CRT)?) CRT stands for "Cathode Ray Tube". CRT代表“ 陰極射線管” 。 Cathode Ray Tube is a technology that is used widely in the traditional televisions and screens.…

mysql8安裝目錄linux7.5_Linux系統下 MySQL 5.7和8.0 版本安裝指南

一. 準備工作1 刪除本地CentOS7中的mariadb&#xff1a;查看系統中是否已安裝 mariadb 服務&#xff1a;rpm -qa | grep mariadb或yum list installed | grep mariadb如果已安裝則刪除 mariadb及其依賴的包&#xff1a;yum -y remove mariadb-libs-5.5.44-2.el7.centos.x86_64關…

ruby array_Ruby中帶有示例的Array.fill()方法(1)

ruby arrayArray.fill()方法 (Array.fill() Method) In this article, we will study about Array.fill() method. You all must be thinking the method must be doing something related to populate the Array instance. Well, we will figure this out in the rest of our …

python二分法查找程序_Python程序查找最大EVEN數

python二分法查找程序Input N integer numbers and we have to find the maximum even number. 輸入N個整數&#xff0c;我們必須找到最大的偶數。 There are many ways of doing this but this time, we have to thought of most computationally efficient algorithm to do …

如何快速精確的和leader溝通

2019獨角獸企業重金招聘Python工程師標準>>> 【緣起】 一個同學找我討論個事情&#xff0c;溝通了一會還是不確定要表達什么&#xff0c;希望我配合什么。結合自己的經驗&#xff0c;簡單的聊聊“如何快速精準的和leader溝通一件事”。 【員工角度的潛在困惑&#x…

java字符串最長回文串_Java中的字符串回文程序

java字符串最長回文串Given a string and we have to check whether it is palindrome string or not. 給定一個字符串&#xff0c;我們必須檢查它是否是回文字符串。 A string that is equal to its reverse string is known as palindrome string. To implement the program…

UOJ#31 【UR #2】豬豬俠再戰括號序列

傳送門http://uoj.ac/problem/31 大家好我是來自百度貼吧的_叫我豬豬俠&#xff0c;英文名叫_CallMeGGBond。 我不曾上過大學&#xff0c;但這不影響我對離散數學、復雜性分析等領域的興趣&#xff1b;尤其是括號序列理論&#xff0c;一度令我沉浸其中&#xff0c;無法自拔。至…

li怎么讓文字在圖片下面_div+css(ul li)實現圖片上文字下列表布局

css樣式表代碼&#xff1a;html布局代碼&#xff1a;效果圖&#xff1a;html布局部分&#xff0c;可根據自己需要添加對應的div即可。1、CSS關鍵樣式單詞解釋1)、ul.imglist{ margin:0 auto; width:536px; overflow:hidden}使用margin:0 auto&#xff0c;讓ul結構布局居中&…

如何使用React Native樣式表?

Without wasting much time, a style sheet as commonly known in a CSS is an object or block of code of many styling properties and values which is applied in a code when called. 在不浪費大量時間的情況下&#xff0c;CSS中通常已知的樣式表是具有許多樣式屬性和值的…

【iCore1S 雙核心板_ARM】例程三:EXTI中斷輸入實驗——讀取ARM按鍵狀態

實驗原理&#xff1a; 按鍵的一端與STM32的GPIO(PB9)相連&#xff0c;且PB9外接一個1k大小的限流上接電阻。 初始化時把PB9設置成輸入模式&#xff0c;當按鍵彈起時&#xff0c;PB9由于上拉電阻的作用呈高電平&#xff08;3.3V&#xff09;&#xff1b; 當按鍵按下時&#xff0…

MySQL小黑框怎么打開_打開你的小黑框命令行,來跟我一起嗨嗨嗨

文章更新于2020-03-16關于電腦位數&#xff1a;位數代表cpu可尋址的內存地址大小。32位的cpu最多可使用4GB內存&#xff0c;而64位cpu能處理的內存范圍就高多了。操作系統也類似&#xff0c;只要看到操作系統里面能識別8GB內存就可以知道cpu和操作系統都是64位。一、常用的 cmd…

您如何從Python的stdin中讀取信息?

Python supports following ways to read an input from stdin (standard input), Python支持以下方式從stdin(標準輸入)讀取輸入 &#xff0c; 1)使用sys.stdin (1) Using sys.stdin) sys.stdin is a file-like object on which we can call functions read() or readlines()…

CentOS7下的AIDE***檢測配置

1、AIDE的簡單介紹AIDE通過掃描一臺&#xff08;未被篡改&#xff09;的Linux服務器的文件系統來構建文件屬性數據庫&#xff0c;以后將服務器文件屬性與數據庫中的進行校對&#xff0c;然后在服務器運行時對被修改的索引了的文件發出警告。出于這個原因&#xff0c;AIDE必須在…

mysql主從不同步 tar_Mysql主從不同步問題處理案例

在使用Mysql的主從復制架構中&#xff0c;有兩個比較頭疼的問題&#xff1a;1、主從數據不同步后如何處理2、主從同步延遲問題如何解決本文將根據實際案例來分析下問題1&#xff0c;至于問題2多數文檔介紹的辦法是啟用多線程復制來解決&#xff0c;言歸正傳&#xff0c;這里的問…

編程語言優缺點_R編程語言的優缺點

編程語言優缺點In general, the R programming language is considered as the machine learning language. This is widely employed in the applications where the data analysis, visualization, and the sampling process are involved. The R programming language is ta…

mysql重做日志與binlog日志區別_MySQL中的重做日志(redo log),回滾日志(undo log),以及二進制日志(binlog)的簡單總結...

MySQL中有六種日志文件&#xff0c;分別是重做日志(redo log)回滾日志(undo log)二進制日志(binlog)錯誤日志(errorlog)慢查詢日志(slow query log)一般查詢日志(general log)中繼日志(relay log)。其中重做日志和回滾日志與事務操作息息相關&#xff0c;二進制日志也與事務操作…