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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

發布時間:2023/12/1 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ruby hash方法_Ruby中带有示例的Hash.length方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ruby hash方法

哈希長度方法 (Hash.length Method)

In this article, we will study about Hash.length 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.length方法 。 可以借助其名稱來預測此方法的工作,但是它并不像看起來那樣簡單。 好了,我們將在其余內容中借助其語法和程序代碼來理解此方法。

Method description:

方法說明:

This method is a public instance method that is defined in the ruby library especially for Hash class. This method works in a way that it iterates over the whole hash object and gives the number of key-value pairs that are present in the hash object. This method will return 0 if it finds the absence of any of the keys after successfully traversing the hash object.

此方法是在ruby庫中定義的公共實例方法,特別是針對Hash類。 此方法的工作方式是遍歷整個哈希對象,并給出哈希對象中存在的鍵值對的數量。 如果在成功遍歷哈希對象后發現缺少任何鍵,則此方法將返回0。

Syntax:

句法:

Hash_object.length

Argument(s) required:

所需參數:

This method does not require any argument. This method simply returns the length of the hash instance.

此方法不需要任何參數。 此方法僅返回哈希實例的長度。

Example 1:

范例1:

=beginRuby program to demonstrate length method =end hash1={"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}puts "Hash length implementation"cnt = hash1.lengthputs "Length of hash object is :#{cnt}"puts "Self hash object : #{hash1}"

Output

輸出量

Hash length implementation Length of hash object is :5 Self hash object : {"color"=>"Black", "object"=>"car", "love"=>"friends", "fruit"=>"Kiwi", "vege"=>"potato"}

Explanation:

說明:

In the above code, you can observe that we are finding the length of the hash object with the help of the Hash.length() method. You can see that the numbers of keys present in the hash object are 5 and the method is returning five after traversing the whole hash object.

在上面的代碼中,您可以觀察到,借助于Hash.length()方法 ,我們正在找到哈希對象的長度。 您可以看到哈希對象中存在的鍵數為5,并且遍歷整個哈希對象后該方法返回5。

Example 2:

范例2:

=beginRuby program to demonstrate length method =end hash1= Hash.new { |hash, key| hash[key] = "Not present" }puts "Hash length implementation"cnt = hash1.lengthputs "Number of keys present in the hash :#{cnt}"puts "Self hash object : #{hash1}"

Output

輸出量

Hash length implementation Number of keys present in the hash :0 Self hash object : {}

Explanation:

說明:

In the above code, you can observe that we are finding the length of the hash object with the help of the Hash.length() method. You can see that the number of keys present in the hash object is 0 and the method is returning zero because the hash on which the method has been invoked is an empty hash.

在上面的代碼中,您可以觀察到,借助于Hash.length()方法 ,我們正在找到哈希對象的長度。 您可以看到哈希對象中存在的鍵數為0,并且該方法返回零,因為在其上調用該方法的哈希是一個空哈希

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

ruby hash方法

總結

以上是生活随笔為你收集整理的ruby hash方法_Ruby中带有示例的Hash.length方法的全部內容,希望文章能夠幫你解決所遇到的問題。

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