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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

ruby hash方法_Ruby中带有示例的Hash.keys方法

發布時間:2025/3/11 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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.keys

Example 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方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。