ruby 生成哈希值_如何检查Ruby中是否存在哈希键?

ruby 生成哈希值

We know that Hashes are the instances which are the combination of keys and their values. A particular key in hash may contain more than one value as well. In this article, we will see how we can check or test the presence of a particular key in hash object? There are multiple ways to tackle this problem.

我们知道哈希是键及其值组合的实例。 哈希中的特定键也可能包含多个值。 在本文中,我们将看到如何检查或测试哈希对象中特定键的存在? 有多种方法可以解决此问题。

Let us discuss a few of them,

让我们讨论其中的一些,

Method 1: With the help of has_key?() method

方法1:借助has_key?()方法

This method is a Public instance method and belongs to the Hash class which lives inside the library of Ruby language. Hash.has_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.has_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.has_key?(obj)

Parameter(s) required:

所需参数:

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

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

Program:

程序:

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

Output

输出量

Hash.has_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.has_key?() method on the normal Hash instance. It has returned true when it found the presence of the key in the Hash object which is entered by the user.

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

Method 2: With the help of assoc() method

方法2:借助于assoc()方法

The above method will only tell you the presence of key but assoc() method will return the set of values associated with that particular key.

上面的方法只会告诉您键的存在,而assoc()方法将返回与该特定键关联的一组值。

This method is a public instance method and belongs to the Hash class which lives inside the library of Ruby language. This method is used to check whether an object(key-value) is a part of the particular Hash instance or not and that Hash instance can or cannot be the normal Hash instance. If it is not normal, it means that Hash instance is the Hash of multiple Array instances along with their keys or you can say that it the collection of multiple keys and values which are itself an object of Array class. Let us go through the syntax and demonstrating the program codes of this method.

该方法是一个公共实例方法,属于Hash类,该类存在于Ruby语言库中。 此方法用于检查对象(键值)是否是特定Hash实例的一部分,以及该Hash实例可以是也可以不是普通Hash实例。 如果不正常,则表示Hash实例是多个Array实例及其键的哈希,或者可以说它是多个键和值的集合,而这些键和值本身就是Array类的对象。 让我们来看一下语法,并演示该方法的程序代码。

If you are thinking about what it will return then let me tell you, it will return the first contained Hash instance where it found the presence of the Key-value pair. It will return 'nil' if it hadn't found the object in any of the Hashes.

如果您正在考虑它将返回什么,那么让我告诉您,它将返回第一个包含的Hash实例,在该实例中找到键值对。 如果未在任何哈希中找到对象,它将返回“ nil ”。

Syntax:

句法:

    Hash_instance.assoc(obj)

Parameter(s) required:

所需参数:

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

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

Program:

程序:

=begin
Ruby program to demonstrate Hash.assoc() method
=end
hsh = {"colors"  => ["red", "blue", "green"],
"letters" => ["a", "b", "c" ], "Fruit" => ["Banana","Kiwi","Grapes"]}
puts "Hash.assoc implementation:"
puts "Enter the Key you want to search:-"
ky = gets.chomp
if (hsh.assoc(ky))
puts "Key found successfully"
puts "Values are: #{hsh.assoc(ky)}"
else
puts "Key not found!"
end

Output

输出量

Hash.assoc implementation:
Enter the Key you want to search:-
colors
Key found successfully
Values are: ["colors", ["red", "blue", "green"]]

Method 3 : With the help of member?() method

方法3:借助member?()方法

This method is a public instance method and belongs to the Hash class which lives inside the library of Ruby language. Hash.member?() 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 the 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.

该方法是一个公共实例方法,属于Hash类,该类存在于Ruby语言库中。 Hash.member?()方法用于检查键(键值)是否为特定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.member?(obj)

Parameter(s) required:

所需参数:

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

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

Program:

程序:

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

Output

输出量

Hash.member? 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.member?() method on the normal Hash instance. It has returned true when it found the presence of the key in the Hash object which is entered by the user.

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

翻译自: https://www.includehelp.com/ruby/how-to-check-if-a-hash-key-exists-in-ruby.aspx

ruby 生成哈希值

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

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

相关文章

C语言实现线性动态(单向)链表【详细步骤】

文章目录什么是链表为什么不用结构体数组链表的操作创建表删除元素插入元素代码及运行结果什么是链表 链表是数据结构里面的一种,线性链表是链表的一种,线性链表的延伸有双向链表和环形链表。在编程语言中优化数据结构可以在处理大数据时大大降低程序的…

移动前端经验小结

1. 移动端头部标签 head meta <!DOCTYPE html> <!-- 使用 HTML5 doctype&#xff0c;不区分大小写 --> <html lang"zh-cmn-Hans"> <!-- 更加标准的 lang 属性写法 http://zhi.hu/XyIa --> <head><!-- 声明文档使用的字符编码 -->…

再见收费的Navicat!操作所有数据库靠它就够了!

为了快速管理数据库&#xff0c;我们一般都会选择一款顺手的数据库管理工具。Navicat、DataGrip虽然很好用&#xff0c;但都是收费的。今天给大家推荐一款免费、功能强大的数据库管理工具DBeaver&#xff0c;希望对大家有所帮助&#xff01;DBeaver简介 DBeaver是一款开源的数据…

查找两个字符串中相同字符串_使两个字符串相同的最低成本

查找两个字符串中相同字符串Problem statement: 问题陈述&#xff1a; Given two strings string1 and string2 find the minimum cost required to make the given two strings identical. We can delete characters from both the strings. The cost of deleting a characte…

Matlab对指定参数的曲线进行非线性拟合

Matlab拟合曲线的方式 Matlab拟合曲线的方式有很多种&#xff0c;有三次样条插值、线性插值、多项式拟合等等。多项式拟合由于函数由f(x)anxnan−1xn−1...a1xa0f(x)a_nx^na_{n-1}x^{n-1}...a_1xa_0f(x)an​xnan−1​xn−1...a1​xa0​组成&#xff0c;若采用最小二乘法拟合&a…

MyBatis原生批量插入的坑与解决方案!

作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;前面的文章咱们讲了 MyBatis 批量插入的 3 种方法&#xff1a;循环单次插入、MyBatis Plus 批量插入、MyBatis 原生批量插入…

Spring学习(20)--- Schema-based AOP(基于配置的AOP实现) -- 配置切入点pointcut

pointcut&#xff08;切断点&#xff09;表达式&#xff1a; execution(public * *(..)) execution(* set*(..)) execution(* com.xyz.service.AccountService.*(..)) execution(* com.xyz.service..(..)) execution(* com.xyz.service...(..)) within(com.xyz.service.*)…

系统结构图 数据结构_数据结构图简介

系统结构图 数据结构What you are going to learn? 你要学什么&#xff1f; In this article, we learn about the introduction to Graphs in Data Structure and Algorithm. 在本文中&#xff0c;我们将了解图在数据结构和算法中的介绍 。 What are the components in Gra…

Matlab仿真PID控制(带M文件、simulink截图和参数分析)

文章目录0.符号说明1.如何根据连续系统建立差分方程1.1.获取连续系统的传递函数1.2.获取离散系统的传递函数1.3.转换为差分方程2.基本PID控制原理3.比较PID输出&#xff0c;分析参数产生的影响4.改进PID算法&#xff08;遇限削弱积分法&#xff09;5.simulink仿真0.符号说明 y…

再见 Postman!Apifox 才是 YYDS!

作为开软件开发从业者&#xff0c;API 调试是必不可少的一项技能&#xff0c;在这方面 Postman 做的非常出色。但是在整个软件开发过程中&#xff0c;API 调试只是其中的一部分&#xff0c;还有很多事情 Postman 无法完成&#xff0c;或者无法高效完成&#xff0c;比如&#xf…

Python operator.not_()函数与示例

operator.not_()函数 (operator.not_() Function) operator.not_() function is a library function of operator module, it is used to perform "NOT operation" on a given value and returns True if the given value is zero or any falsy value, False, otherw…

jprofiler安装与使用

1&#xff1a; 修改/etc/profile 增加以下内容&#xff1a; JPROFILER_HOME/opt/jprofiler9/bin/linux-x64export LD_LIBRARY_PATH$LD_LIBRARY_PATH:$JPROFILER_HOME 2&#xff1a;tomcat /bin/catalina.sh 增加以下内容&#xff1a; CATALINA_OPTS"$CATALINA_OPTS -Xms12…

Matlab【可视化作图】绘制线电压相电压辅助线

目录引言绘图原理采点绘图设置坐标轴标尺引言 学习电力电子的同学可能在私下里练习的时候非常需要三相线电压和相电压的辅助线。最近我随便找了一本书把Matlab可视化编程恶补了一下&#xff0c;给大家介绍一下这个波形辅助线是怎么做的。 三相线电压辅助线就是一组相位相差60的…

SpringBoot实现Excel导入导出,好用到爆,POI可以扔掉了!

在我们平时工作中经常会遇到要操作Excel的功能&#xff0c;比如导出个用户信息或者订单信息的Excel报表。你肯定听说过POI这个东西&#xff0c;可以实现。但是POI实现的API确实很麻烦&#xff0c;它需要写那种逐行解析的代码&#xff08;类似Xml解析&#xff09;。今天给大家推…

Python datetime __str __()方法与示例

Python datetime .__ str __()方法 (Python datetime.__str__() Method) datetime.__str__() method is used to manipulate objects of datetime class of module datetime. datetime .__ str __()方法用于操作模块datetime的datetime类的对象。 It uses a datetime class o…

Facebook升级到MySQL 8.0付出的代价

近日&#xff0c;Facebook 官博公布了他们的数据库版本从 MySQL 5.6 升级到了 MySQL 8.0&#xff0c;并且在官博记录了复盘详细的升级过程。Facebook 称&#xff0c;他们最近的一次大版本升级到 MySQL 5.6 花了一年多时间才完成&#xff0c;还在 5.6 版上开发 LSM 树存储引擎&a…

Matlab制作朱利表

朱利判据 其中 {bn−kan−k−ana0∗akcn−kbn−k−bnb0∗bk...qn−kpn−k−pnp0∗pk\begin{cases} b_{n-k}a_{n-k}-\frac{a_n}{a_0}*a_k\\ c_{n-k}b_{n-k}-\frac{b_n}{b_0}*b_k\\ ...\\ q_{n-k}p_{n-k}-\frac{p_n}{p_0}*p_k \end{cases}⎩⎪⎪⎪⎨⎪⎪⎪⎧​bn−k​an−k​−a0…

HDOJ 1047 Integer Inquiry

JAVA睑板.... Integer Inquiry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12042 Accepted Submission(s): 3037Problem DescriptionOne of the first users of BITs new supercomputer was Chip Diller. …

高并发下秒杀商品,必须知道的9个细节

高并发下如何设计秒杀系统&#xff1f;这是一个高频面试题。这个问题看似简单&#xff0c;但是里面的水很深&#xff0c;它考查的是高并发场景下&#xff0c;从前端到后端多方面的知识。秒杀一般出现在商城的促销活动中&#xff0c;指定了一定数量&#xff08;比如&#xff1a;…

weakhashmap_Java WeakHashMap keySet()方法与示例

weakhashmapWeakHashMap类的keySet()方法 (WeakHashMap Class keySet() method) keySet() method is available in java.util package. keySet()方法在java.util包中可用。 keySet() method is used to retrieve all the key exists in this map to be viewed in a set. keySet…