ruby hash方法_Ruby中带有示例的Hash.key?(obj)方法

ruby hash方法

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

In this article, we will study about Hash.key?(obj) 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.key?(obj)方法 。 由于该方法的名称完全不同,因此无法进行假设。 让我们阅读其定义并在语法和程序代码的帮助下了解其实现。

Method description:

方法说明:

This method is a Public instance method and belongs to the Hash class which lives inside the library of Ruby language. Hash.key?() method is used to check whether a key(key-value) is a part of the particular Hash instance or not and that Hash instance should be a normal Hash instance. It will search through the whole Hash and gives you the result according to its search. Let us go through the syntax and demonstrating the program codes of this method.

此方法是Public实例方法,属于Hash类,它位于Ruby语言库中。 Hash.key?()方法用于检查键(键值)是否为特定Hash实例的一部分,并且该Hash实例应为普通Hash实例。 它将搜索整个哈希,并根据其搜索结果。 让我们来看一下语法,并演示该方法的程序代码。

If you are thinking about what it will return then let me tell you, it will return a Boolean value. The returned value will be true if it finds the key inside the Hash and the return value will be false if it does not find the key to be the part of Hash instance.

如果您正在考虑它将返回什么,那么让我告诉您, 它将返回一个布尔值 。 如果在哈希表中找到密钥,则返回值将为true;如果找不到哈希表实例的一部分,则返回值为false。

Syntax:

句法:

    Hash_instance.key?(obj)

Argument(s) required:

所需参数:

This method only takes one parameter and that argument is nothing but the key whose presence we want to check.

此方法仅使用一个参数,而该参数不过是我们要检查其存在性的键。

Example 1:

范例1:

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

Output

输出量

Hash.key? implementation:
Enter the Key you want to search: 
colors
Key found successfully

Explanation:

说明:

In the above code, you can observe that we are invoking the Hash.key?() method on the normal Hash instance. It has returned true when it found the presence of key in the Hash object which is entered by the user.

在上面的代码中,您可以观察到我们在普通的Hash实例上调用Hash.key?()方法 。 当它在用户输入的哈希对象中发现密钥存在时,它返回true。

Example 2:

范例2:

=begin
Ruby program to demonstrate Hash.key? method
=end	
hsh = {"colors"  => "red","letters" => "a", "Fruit" => "Grapes"}
hsh1 = {"cars"  => "800","bike" => "pulsar", "phone" => "A50"}
hsh2 = {"one"=> hsh, "two" => hsh1}
puts "Hash.key? implementation:"
puts "Enter the Key you want to search: "
ky = gets.chomp
if (hsh2.key?(ky))
puts "Key found successfully"
else
puts "Key not found!"
end

Output

输出量

Hash.key? implementation:
Enter the Key you want to search: 
colors
Key not found!

Explanation:

说明:

In the above code, you can verify that Hash.key?() method does not work upon Hash instance which is the collection of multiple Hash instances. It will return false even if the object is a part of the Hash instance.

在上面的代码中,您可以验证Hash.key?()方法不适用于Hash实例,该实例是多​​个Hash实例的集合。 即使对象是Hash实例的一部分,它也会返回false。

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

ruby hash方法

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

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

相关文章

python迭代器与生成器答案_史上最全 Python 迭代器与生成器

原标题:史上最全 Python 迭代器与生成器作者:浪子燕青链接:http://www.langzi.fun/迭代器与生成器.html迭代器与可迭代对象概念迭代器:是访问数据集合内元素的一种方式,一般用来遍历数据,但是他不能像列表一…

[性能测试] LoadRunner结果分析 – TPS

本文转载自:http://www.tuicool.com/articles/6z6vuy针对吞吐率和 TPS 的关系,这个在结果分析中如何使用,就个人经验和朋友讨论后,提出如下建议指导,欢迎同僚指正。相关定义响应时间 网络响应时间 应用程序响应时间响…

密码学电子书_密码学中的电子密码书(ECB)

密码学电子书This Electronic Code Book (ECB) is cryptography as a mode of operation for a block cipher, with the characters the main things that every feasible block of plaintext or an original text has a corresponding characteristic of ciphertext value and…

tsql是mysql中的吗_Mysql中的sql是如何执行的

MySQL中的SQL是如何执行的MySQL是典型的C/S架构,也就是Client/Server架构,服务器端程序使用的mysqld.整体的MySQL流程如下图所示:MySQL是有三层组成:连接层: 负责客户端与服务器端建立连接,客户端发送SQL至服务端;SQL层: 对SQL语句进行查询处理;存储引擎层: 与数据库文件打交道…

软件质量特性测试

针对软件质量特性进行测试,可以避免重大漏测,一般人我不告诉他。《软件工程—产品质量》(GB/T 16260-2006)中规定对软件的每个质量特性与子特性都有定义:一、功能性:是指当软件在指定条件下使用&#xff0c…

PHP array_pop()函数与示例

PHP array_pop()函数 (PHP array_pop() function) array_pop() function is used to delete/pop last element from the array. array_pop()函数用于从数组中删除/弹出最后一个元素。 Syntax: 句法: array_pop(array);Here, array is the input array, function w…

网站关停就没事了?5100万账户文件被盗

曾经是美国三大音乐视频文件共享软件之一的imesh,意外倒闭。而更意外的是,就在近日,imesh这款已经倒闭的软件,5100万账户开始在暗网被黑客拍卖。 Imesh这款软件是美国纽约的老牌音乐视频分享软件之一,早在2000年前便已…

数据库表设计索引外键设计_关于索引的设计决策 数据库管理系统

数据库表设计索引外键设计Introduction: 介绍: The attributes whose values are required inequality or range conditions and those that are keys or that participate in join conditions require access paths. 其值为必需的不等式或范围条件的属性以及作为键…

接口测试从零开始系列_mock技术使用

1、什么情况下会使用mock技术 (1)需要将当前被测单元和其依赖模块独立开来,构造一个独立的测试环境,不关注被测单元的依赖对象,只关注被测单元的功能逻辑 ----------比如被测代码中需要依赖第三方接口返回值进行逻辑处…

amie 规则挖掘_AMIE的完整形式是什么?

amie 规则挖掘AMIE:工程师协会的准会员 (AMIE: Associate Member of the Institution of Engineers) AMIE is an abbreviation of Associate Member of the Institution of Engineers. The Institution of Engineers India Limited (IEIL) provides this profession…

java 马克思_单链表-Java

public class SinglyListNode {int val;SinglyListNode next;SinglyListNode() {}SinglyListNode(int x) {this.val x;}}/*执行用时:12 ms, 在所有 Java 提交中击败了66.93%的用户内存消耗:39.5 MB, 在所有 Java 提交中击败了5.06%的用户*/class MyLink…

python的pass语句_Python | 演示pass语句的示例

python的pass语句python中的pass语句 (pass statement in python) "pass" is a type of null operation or null statement, when it executes nothing happens. It is used when you want do not want to write any code/statement to execute but syntactically a …

HDS:聚焦未来的投资“冻结”

一家日本IT网站报道的有关HDS冻结对高端存储产品的投资一事引发众议。让人陷入疑惑的这次声明就是,HDS认为单纯的阵列产品并非企业存储的未来。 6月1日,IT Pro Nikkei网站发布了一篇报道,内容援引HDS一份表示将冻结高端存储业务的简报。这引发…

java js对象转字符串数组_JS数组转字符串(3种方法)【转】

JavaScript 允许数组与字符串之间相互转换。其中 Array 方法对象定义了 3 个方法,可以把数组转换为字符串,如表所示。数组方法说明toString()将数组转换成一个字符串toLocalString()把数组转换成本地约定的字符串join()将数组元素连接起来以构建一个字符…

中美共建大数据创新研究中心

由贵阳市人民政府、工信部电子一所、美国加州大学伯克利分校合作共建的贵州伯克利大数据创新研究中心日前在贵阳揭牌。 据了解,贵州伯克利大数据创新研究中心将分两阶段建设。第一阶段,2016年9月份至2017年底,将重点完成“学龄儿童大数据分析…

Python中的__init__和self是做什么的?

The __init__ and self are two keywords in python, which performs a vital role in the application. __init__和self是python中的两个关键字,在应用程序中起着至关重要的作用。 To begin with, it is important to understand the concept of class and object…

Palo Alto Networks漏洞防护扩展至云端

中国北京,2016年4月12日 –下一代安全企业Palo Alto Networks?(纽交所代码:PANW)近日宣布进一步增强其下一代安全平台,扩展漏洞防护能力,以满足那些依赖云环境和SaaS应用的业务对安全的需求。 企业机构需要变得更加灵活和有竞争力…

java 嵌套调用_Java嵌套类的使用

嵌套类是指被定义在另一个类内部的类,它为外部类提供服务。嵌套类分四种:静态成员类、非静态成员类、匿名类和局部类。一、静态成员类与非静态成员类的区别?在什么情况下可以用静态成员类?我们知道在类的设计中,为了避…

c语言getenv函数_getenv()函数与C ++中的示例

c语言getenv函数C getenv()函数 (C getenv() function) getenv() function is a library function of cstdlib header. It is used to get the environment string. It accepts a parameter which is an environment variable name (platform dependent, it may either case s…

isless()函数与C ++中的示例

C isless()函数 (C isless() function) isless() function is a library function of cmath header, it is used to check whether the given first value is less than the second value. It accepts two values (float, double or long double) and returns 1 if the first …