ruby hash方法_Ruby中带有示例的Hash.keys方法
ruby hash方法
哈希鍵方法 (Hash.keys Method)
In this article, we will study about Hash.keys Method. The working of the method can't be assumed because of it's quite a different name. Let us read its definition and understand its implementation with the help of syntax and program codes.
在本文中,我們將研究Hash.keys方法 。 由于該方法的名稱完全不同,因此無法進行假設。 讓我們閱讀其定義并在語法和程序代碼的幫助下了解其實現。
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 an array contacting all the keys which are present in the hash object. In simpler language, you can say that the new array will be containing the keys from the self hash object. It will return an empty array if no values are present in the hash objects.
此方法是Public實例方法,屬于Hash類,它位于Ruby語言庫中。 此方法的工作方式是返回一個數組,該數組聯系哈希對象中存在的所有鍵。 用更簡單的語言,您可以說新數組將包含來自自我哈希對象的鍵。 如果哈希對象中沒有值,它將返回一個空數組。
Syntax:
句法:
Hash.keysExample 1:
范例1:
=beginRuby program to demonstrate Hash.keys method =end hsh = {"colors" => "red","letters" => "a", "Fruit" => "Grapes", "anything"=>"red","sweet"=>"ladoo"}puts "Hash.keys implementation:" ary = hsh.keysputs "The keys present in the hash are: #{ary}"puts "Self hash object: #{hsh}"Output
輸出量
Hash.keys implementation: The keys present in the hash are: ["colors", "letters", "Fruit", "anything", "sweet"] Self hash object: {"colors"=>"red", "letters"=>"a", "Fruit"=>"Grapes", "anything"=>"red", "sweet"=>"ladoo"}Explanation:
說明:
In the above code, you can observe that you can find all the keys present in the hash with the help of the Hash.keys method. The array instance returned from the hash will be populated with all the keys present in the specific hash object. This is very clear with the help of the output drawn from the program code given for the demonstration purpose.
在上面的代碼中,您可以觀察到借助Hash.keys方法可以找到哈希中存在的所有鍵。 從哈希返回的數組實例將使用特定哈希對象中存在的所有鍵填充。 借助于為演示目的給出的程序代碼得出的輸出,這一點非常清楚。
Example 2:
范例2:
=beginRuby program to demonstrate Hash.keys method =end hsh = Hash.new()puts "Hash.keys implementation:"ary = hsh.keysputs "The keys present in the hash are: #{ary}"puts "Self hash object: #{hsh}"Output
輸出量
Hash.keys implementation: The keys present in the hash are: [] Self hash object: {}Explanation:
說明:
In the above code, you can observe that we are trying to fetch all the keys from the hash with the help of the Hash.keys method. This method is returning an empty array if the hash is empty or you can say that it doesn't have any key-value pair. This is very clear with the help of the output drawn by the program code.
在上面的代碼中,您可以觀察到我們正在嘗試借助Hash.keys方法從哈希中獲取所有鍵。 如果哈希為空,或者您可以說它沒有任何鍵值對,則此方法將返回一個空數組。 借助于程序代碼繪制的輸出,這非常清楚。
翻譯自: https://www.includehelp.com/ruby/hash-keys-method-with-example.aspx
ruby hash方法
總結
以上是生活随笔為你收集整理的ruby hash方法_Ruby中带有示例的Hash.keys方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows php5.3升级,Win
- 下一篇: airplay2协议是什么_什么是Air