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方法 。 可以借助其名稱來預(yù)測此方法的工作,但是它并不像看起來那樣簡單。 好了,我們將在其余內(nèi)容中借助其語法和程序代碼來理解此方法。
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庫中定義的公共實(shí)例方法,尤其是針對Hash類。 此方法的工作方式是,針對哈希對象中存在的每個(gè)單個(gè)鍵至少調(diào)用一次該塊。 哈希對象中存在的鍵值對作為參數(shù)傳遞。 如果不提供任何塊,則應(yīng)期望枚舉數(shù)作為each_pair方法的返回值。
Syntax:
句法:
Hash_object.each_pair{|key,value| block}Argument(s) required:
所需參數(shù):
This method does not accept any arguments. However, you can pass a block along with this method.
此方法不接受任何參數(shù)。 但是,您可以通過此方法傳遞一個(gè)塊。
Example 1:
范例1:
=beginRuby program to demonstrate each_pair method =endhsh={"name"=>"Zorawar","class"=>"ukg","school"=>"AASSC","place"=>"Haridwar"}puts "Hash each_pair implementation"str = hsh.each_pair{|key,value| puts "#{key} is #{value}"} puts strOutput
輸出量
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.
在上面的代碼中,您可能會(huì)看到,借助于Hash.each_pair方法 ,我們正在從哈希對象中打印每個(gè)鍵值對。 該方法正在調(diào)用一個(gè)塊,該塊將鍵值作為哈希對象的參數(shù)。 該方法遍歷哈希對象,對其進(jìn)行處理并為我們提供所需的輸出,或者您可以說讓我們知道存儲(chǔ)在特定鍵中的值。
Example 2:
范例2:
=beginRuby program to demonstrate each_pair method =endhsh={"name"=>"Zorawar","class"=>"ukg","school"=>"AASSC","place"=>"Haridwar"}puts "Hash each_pair implementation"str = hsh.each_pair puts strOutput
輸出量
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.
在上面的代碼中,您可以觀察到,當(dāng)我們在不使用該塊的情況下調(diào)用該方法時(shí),就會(huì)得到該方法返回的枚舉數(shù)。
翻譯自: https://www.includehelp.com/ruby/hash-each_pair-method-with-example.aspx
as_hash ruby
總結(jié)
以上是生活随笔為你收集整理的as_hash ruby_Ruby中带有示例的Hash.each_pair方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sdram trp_TRP的完整形式是什
- 下一篇: duration java_Java D