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:

=begin
  Ruby program to demonstrate length method
=end	

hash1={"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}

puts "Hash length implementation"

cnt = hash1.length

puts  "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:

=begin
  Ruby program to demonstrate length method
=end	

hash1= Hash.new { |hash, key| hash[key] = "Not present" }

puts "Hash length implementation"

cnt = hash1.length

puts "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方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值