Ruby中带有示例的Hash.key?(value)方法

Hash.key?(value)方法 (Hash.key?(value) Method)

In this article, we will study about Hash.key?(value) Method. The working of the method can't be assumed because it's quite a different name. Let us read its definition and understand its implementation with the help of syntax and program codes.

在本文中,我们将研究Hash.key?(value)方法 。 无法假定该方法的工作原理,因为它的名称完全不同。 让我们阅读其定义并在语法和程序代码的帮助下了解其实现。

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 checks the presence of a particular value in the hash object and returns it's key if the value is present in the hash. This value will return nil if it doesn't find the presence of value which is asked by the user which is passed with the method at the time of its invocation.

此方法是Public实例方法,属于Hash类,它位于Ruby语言库中。 此方法的工作方式是检查哈希对象中是否存在特定值,如果哈希中存在该值,则返回其键。 如果找不到调用该方法时传递的用户要求的值,则此值将返回nil。

Syntax:

句法:

    Hash.key(value)

Argument(s) required:

所需参数:

This method only requires one argument and that value is nothing but the value of the key you want to get.

此方法仅需要一个参数,并且该值仅是您要获取的键的值。

Example 1:

范例1:

=begin
Ruby program to demonstrate Hash.key(value) method
=end	
hsh = {"colors"  => "red","letters" => "a", "Fruit" => "Grapes"}
puts "Hash.key(value) implementation:"
puts "Enter the value you want to search:"
value = gets.chomp
if (hsh.key(value))
puts "Value found successfully key is #{hsh.key(value)}"
else
puts "Key not found!"
end

Output

输出量

Hash.key(value) implementation:
Enter the value you want to search:
red
Value found successfully key is colors

Explanation:

说明:

In the above code, you can observe that we are finding keys with the help of values by using Hash.key(value) method. You can see that we are asking the user for value whose key he/she wants to find. First, we are checking whether the value is present in the hash or not. If it is not present in the hash object then the method will return 'nil' and you will not get anything as the result.

在上面的代码中,您可以观察到我们正在使用Hash.key(value)方法在值的帮助下找到键。 可以看到,我们在向用户询问他/她想找到其键的值。 首先,我们正在检查该值是否存在于哈希中。 如果哈希对象中不存在该对象,则该方法将返回“ nil”,并且您将不会得到任何结果。

Example 2:

范例2:

=begin
Ruby program to demonstrate Hash.key(value) method
=end	
hsh = {"colors"  => "red","letters" => "a", "Fruit" => "Grapes", "anything"=>"red"}
puts "Hash.key(value) implementation:"
puts "Enter the value you want to search:"
value = gets.chomp
if (hsh.key(value))
puts "Value found successfully key is #{hsh.key(value)}"
else
puts "Key not found!"
end

Output

输出量

Hash.key(value) implementation:
Enter the value you want to search:
red
Value found successfully key is colors

Explanation:

说明:

With the help of the above code, it is demonstrated that when there are two keys present in the hash object with the same value then instead of returning both the keys, this method returns only first key in which it has found the presence of value.

借助于以上代码,证明了当哈希对象中存在两个具有相同值的键时,而不是返回两个键,此方法仅返回发现值存在的第一个键。

翻译自: https://www.includehelp.com/ruby/hash-key-value-method-with-example.aspx

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

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

相关文章

计算机课伤害事故的防范,体育课安全伤害事故的防范与处理

摘 要:近年来,在教育改革的背景下,学校方面特别重视体育教学这一环节。做好体育教学工作,一方面能够使学生达到强身健体的目的,另一方面能够使学生的综合素质水平得到有效提升。然而,在体育课中还会发生一些…

同一个容器实例可以同时运行在多个宿主机_从零开始学K8s: 3.什么是容器

Kubernetes使用Linux容器技术来实现应用的隔离。因此在深入学习k8s之前,我们需要先学习容器的基础知识以便更好地去理解k8s的原理机制。揭开容器的神秘面纱当一个应用只由较少数量的大组件构成时,完全可以给每个组件分配一个专用的虚拟机,以及…

__str__是什么函数_PHP str_shuffle()函数与示例

__str__是什么函数PHP str_shuffle()函数 (PHP str_shuffle() Function) str_shuffle() function is a string function and it is used to shuffle all characters randomly in the string. str_shuffle()函数是一个字符串函数,用于随机地随机排列字符串中的所有字…

SpringBoot集成RabbitMq消息队列【附源码】

1. 项目背景 要啥项目背景,就是干!!! SpringBoot版本:2.7.12 2. Rabbit MQ安装 这里讲解使用docker安装RabbitMQ,如果在windows下面安装RabbitMQ,参考下文 【笑小枫的按步照搬系列】Window…

【云栖直播】精彩推荐第3期:个性化推荐系统搭建实践

热门推荐 (1)即将直播持续集成与交付:分层自动化之UI自动化体系建设直播简介:本系列直播由阿里旗下一站式研发提效平台云效策划推出,主要为大家详细介绍阿里巴巴在持续集成和持续交付的最佳实践。 直播讲师&#xff1a…

全国计算机等级考试和cct区别,全国计算机等级考试(NCRE)和CCT的区别你了解多少?...

在大学,很多学生都在纠结,到底学什么技能在以后的就业中能够提升自己的核心竞争力。在众多等级考试中,全国计算机等级考试可以说是相对热门的考试。关于计算机等级考试,一共有两种,一类是全国高等学校计算机水平考试(C…

ensp路由器无法启动_品胜云路由器Breed刷入详细教程,技巧和注意事项,功能大增...

前面发文,介绍过品胜净音云路由器WFR101N功能,硬件配置参数,高清拆解图。主要硬件参数:闪存型号是W25Q128FVSG,容量大小是16M;内存是华邦的W9751G6KB-25,DDR2 SDRAM内存,大小64M&…

第二个冲刺期的第六天

昨天写好了购物车的功能; 今天决定开始写下订单的功能; 明天的话继续写下订单的功能; 遇到的问题:由于购物车是用session实现的,所以生成订单的话会遇到一些问题,比如说订单条目往数据库的写入。 &#xff…

chunk_split_PHP chunk_split()函数与示例

chunk_splitPHP chunk_split()函数 (PHP chunk_split() function) chunk_split() function is used to split the given string into chunks of small parts, it accepts the string returns the parts of the strings specified by the other parameters. chunk_split()函数用…

对计算机视觉研究的认识,计算机视觉(作业)

视觉是各个应用领域,如制造业、检验、文档分析、医疗诊断,和军事等领域中各种智能/自主系统中不可分割的一部分。由于它的重要性,一些先进国家,例如美国把对计算机视觉的研究列为对经济和科学有广泛影响的科学和工程中…

map函数python求数的每位的值_map函数_实现批量计算

作者Toby,持牌照消费金融模型经理,发明国家算法专利,国内最大医药数据中心数据挖掘部门负责人!和重庆儿科医院,中科院教授,赛柏蓝保持慢病数据挖掘项目合作!清华大学,百度,腾讯&…

Flask 视图

写个验证用户登录的装饰器:在调用函数前,先检查session里有没有用户 from functools import wraps from flask import session, abortdef login_required(func):wraps(func)def decorated_function(*args, **kwargs):if not user in session:abort(401)r…

wps计算机打印双面输出,在wps中双面打印的方法步骤详解

我们在打印word文档的时候往往都会使用到双面打印,那么今天小编教你怎么在wps中双面打印。希望对你有帮助!WPS双面打印的步骤首先点击wps上的文字选项。找到打印按钮。点击打开之后,会跳出这样一个页面。PS双面打印的步骤图1页面内&#xff0…

PHP array_merge()函数与示例

PHP array_merge()函数 (PHP array_merge() function) array_merge() function is used to merge two or more arrays, it returns a new array with merged elements. array_merge()函数用于合并两个或多个数组,它返回一个具有合并元素的新数组。 Syntax: 句法&am…

linux pwm 调屏_Linux驱动学习之:PWM驱动

PWM(Pulse Width Modulation)——脉宽调制,它是利用微控制器的数字输出来对模拟电路进行控制的一种非常有效的技术,广泛应用于测量、通信、功率控制与变换等许多领域。s3c2440芯片中一共有5个16位的定时器,其中有4个定时器(定时器0~定时器3)具…

2019聊大考研计算机调剂,2019年聊城大学硕士研究生预调剂工作说明

教育部2019年分数线与招生计划尚未确定,根据往年分数线与报考情况,现将我校2019年硕士研究生预调剂工作说明如下:一、调剂要求:(一)只接受学历为本科的考生调剂,应届本科生优先;(二)初试成绩达到报考专业与拟调剂专业教育部A区分数…

SylixOS磁盘高速传输

SylixOS管线模型分析前文主要介绍了SylixOS中的块设备CACHE管理,本章主要介绍磁盘高速传输。在CAHCE回写中SyilxOS采取了两种方式,即直接回写和多管线并发回写。并发写管线通过多线程并发处理CACHE提交的写请求,实现磁盘高速传输。SylixOS中通…

kotlin int最大值_Kotlin程序查找三个数字中的最大值

kotlin int最大值Input 3 integer numbers, we have to find the largest of these input numbers. 输入3个整数,我们必须找到这些输入数字中最大的一个。 Example: 例: Input:First number: 10Second number: 20Third number: 30Output:Largest numbe…

计算机社团活动展望未来,社团展望未来寄语简短,如何写社团未来计划

呵呵,你不会是社团的会长吧,不会写这个的话说不过去了,而且没有你想象的那么复杂,有条理,分类的写下去就可以了,但这里不要太多的口水话!再者你可以去问一下其他社团的负责人的!&…

判断五个分数等级划分_压力表精度等级怎么算?压力表精度等级划分及检验项目...

压力表是一种典型的轴向精密压力表,是用来测量介质压力的仪表,在工业生产也是常用的仪表,对生产起到了非常重要的作用,压力表的所显示压力的精度,就成了我们非常重要的一个参数。今天小编就来为大家说说压力表精度等级…