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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380636.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【转贴备忘】[教程]iPhone 實機開發 Part 1 - HelloWorld

http://bbs.weiphone.com/read-htm-tid-222380-page-1.html最近小弟開始在研究 iPhone 的開發,無奈找到的資料都是舊的,也不知道怎麼放到 iPhone 上,應該很多人都跟我有同樣的困擾吧!經過幾天的研究總算寫出第一個 HelloWorld,而且可以不用付99元美金就可以放到實機上執行! 今天…

final 在java,final 在java中的注意点

注意事项慢慢积累final 类不可被继承,也不能有子类。final class Book{}//class ComBook extends Book{} 这将编译失败,因为Book类不可被继承。final方法不可被改写class Book{final void show(){System.out.println("Book的show()方法”)&…

ECC加密算法入门介绍

前言 同RSA(Ron Rivest,Adi Shamir,Len Adleman三位天才的名字)一样,ECC(Elliptic Curves Cryptography,椭圆曲线密码编码学)也属于公开密钥算法。目前,国内详细介绍E…

DS和[address]

8086CPU中,用16位寄存器来存储一个字。高8位存放高位字节,低8位存放低位字节。在内存中存储时,由于内存单元是字节单元,则一个字要用两个地址连续的内存单元来存放,这个字的低位存放低地址单元中,高位字节存…

计算机指令格式_计算机科学组织| 指令格式

计算机指令格式指令格式 (Instruction format ) Instruction format describes the internal structures (layout design) of the bits of an instruction, in terms of its constituent parts. 指令格式按照指令的组成部分描述指令位的内部结构(布局设计)。 An Instruction fo…

对JSON的一点认识和理解以及JQuery处理JSON

JSON(JavaScript Object Notation)javscript对象标记,是一种轻量级的数据交换格式。具体的详细基础知识可以参考JSON官网http:/www.json.org/。 不说太多废话,直接说说我对JSON的理解吧。 一、JSON的一些基础知识。 JSON中对象通过“{}”来标识&#xff…

网络硬盘

不知道自己确切从何时对各种IT玩意儿开始极其关注的,我以前不是这样,至少在大学时不是这样,虽然是学计算机的。但现在总是喜欢尝试这些东西,今天试了试Mofile,一个网络硬盘,看起来不错的样子,且…

SUB指令

形式: SUB dest, source功能: 从目的操作数中减去源操作数 .data var1 DWORD 30000h var2 DWORD 10000h .code mov eax,var1 ;EAX 30000h sub eax,var2 ;EAX 20000h几种形式: sub 寄存器,数据 比…

php 查看 实例 的方法,php – 从Laravel 5.1中的通用数据库查询中获取Eloquent模型的实例...

我有不同关系的模型.假设我的Entry模型属于供应商,所以通常我的模型文件中有一个supplier()方法.到目前为止一切都那么好,当我有一些像Entry :: find(1) – >供应商这样的产品时,效果非常好.然而,什么是无效的是当我从Laravel中的通用DB ::查询中获取条目时,我显然无法访问s…

ejs获取js变量值_EJS变量(注入值)

ejs获取js变量值Hi! Welcome to NODE AND EJS TEMPLATE ENGINE SERIES. Today, we will talk about EJS variables or how we can inject values? 嗨! 欢迎使用NODE和EJS模板引擎系列。 今天,我们将讨论EJS变量或如何注入值? Just like nor…

POP和PUSH指令

PUSH指令: 首先减少 ESP 的值,再将源操作数复制到堆栈。操作数是 16 位的,则 ESP 减 2,操作数是 32 位的,则 ESP 减 4。PUSH 指令有 3 种格式: PUSH reg/mem16 PUSH reg/mem32 PUSH inm32POP指令&#xff…

Visual Basic 9.0 前沿播报·静态篇(六)松弛委托和可为空类型语法增强

本期介绍的两个VB9作为数据开发辅助功能的新特性。CLR在.NET 2.0增加了许多关于委托的新特性(但大部分都不为任何语言所支持),其中一个叫做协变的特性允许放松委托与函数绑定时的签名检查(C# 2.0支持)。为了追求更高的…

android_Text

....转载于:https://www.cnblogs.com/dwpcny/archive/2011/03/30/2000239.html

scala 函数调用_在Scala中按名称调用函数

scala 函数调用函数按名称调用 (Functions call by name ) By default, the method of parameter passing in a programming language is "call by value". In this, the parameter is passed to a function which makes a copy of them an operates on them. In Sca…

电脑PHP漏洞啥意思,PHP漏洞详解

几个重要的php.ini选项Register Globalsphp>4.2.0,php.ini的register_globals选项的默认值预设为Off,当register_globals的设定为On时,程序可以接收来自服务器的各种环境变量,包括表单提交的变量,而且由于PHP不必事先初始化变量的值,从而导致很大的安全隐患.例1://check_adm…

3年前的小程序:破解需要delphi IDE 环境的vcl 控件

基本原理:有些vcl组件未注册的话,会显示没有注册的信息,但在设计期间不显示这些信息,表示该组件会检查delphi的ide 环境,解决办法就是让自己的exe带上ide的信息;组件检查ide的办法无非就是使用api查找特定的…

执行POP和PUSH指令后,SS和SP的变化

我们知道push指令是将数据送入栈中,pop指令是将数据从栈顶取出来,8086CPU的入栈和出栈操作都是以字为单位的 比如说将10000H-1000FH这段内存当做栈使用 CPU是通过CS、IP中存放的段地址和偏移地址来知道当前要执行的指令,通过DS和[address]来…

python 文件遍历

1. import os,sys r raw_input(“type a directory name”) for root, dirs, files in os.walk(r): for f in files: print root, os.sep, f;2. import os,sys def walkdir(dirname): try: ls os.listdir(dirname) except: print ‘access deny’ else: for l in ls: temp o…

win7 php 上传文件,在LNMP原来的基础上,win7环境下如何上传PHP文件到Linux环境下...

首先,下载一个WINSCP客户端连接主机后,上传文件到自己的保存目录接着进入数据库添加我们的数据库mysql -uroot -p //这个是进入mysql的命令,但是要是你没有加 ln -s /usr/local/mysql/bin/mysql /usr/bin 的话就要输入下面那一行/usr/loc…

HDFC的完整形式是什么?

HDFC:住房发展金融公司 (HDFC: Housing Development Finance Corporation) HDFC is an abbreviation of Housing Development Finance Corporation. It is a well-known housing expansion finance corporation of India which largely makes available housing loa…