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

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

  1. ruby hash方法_Ruby中带有示例的Hash.invert方法

    ruby hash方法 Hash.invert方法 (Hash.invert Method) In this article, we will study about Hash.invert Meth ...

  2. ruby hash方法_Ruby中带有示例的Hash.select方法

    ruby hash方法 哈希选择方法 (Hash.select Method) In this article, we will study about Hash.select Method. The ...

  3. ruby hash方法_Ruby中带有示例的Hash.flatten方法

    ruby hash方法 哈希平化方法 (Hash.flatten Method) In this article, we will study about Hash.flatten Method. T ...

  4. ruby hash方法_Ruby中带有示例的Hash.rehash方法

    ruby hash方法 Hash.rehash方法 (Hash.rehash Method) In this article, we will study about Hash.rehash Meth ...

  5. ruby hash方法_Ruby中带有示例的Hash.values方法

    ruby hash方法 哈希值方法 (Hash.values Method) In this article, we will study about Hash.values Method. The ...

  6. ruby hash方法_Ruby中带有示例的Hash.default(key = nil)方法

    ruby hash方法 Hash.default(key = nil)方法 (Hash.default(key=nil) Method) In this article, we will study ...

  7. ruby hash方法_Ruby中带有示例的Hash.length方法

    ruby hash方法 哈希长度方法 (Hash.length Method) In this article, we will study about Hash.length Method. The ...

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

    ruby hash方法 Hash.rassoc(obj)方法 (Hash.rassoc(obj) Method) In this article, we will study about Hash.r ...

  9. ruby hash方法_Ruby中带有示例的Hash.keys方法

    ruby hash方法 哈希键方法 (Hash.keys Method) In this article, we will study about Hash.keys Method. The work ...

最新文章

  1. OSI 七层参考模型
  2. 值得期待的.Net Micro Framework 3.0
  3. BitmapFactory: inSampleSize 的一些思考
  4. python列表中的冒号_python 列表中[ ]中冒号‘:’的作用
  5. python pandas爬取网页成绩表格,计算各个类别学分
  6. VIM进阶-模式mode
  7. php工程模式,factory - PHP工程模式如何传入参数
  8. centos mysql导出数据库命令_在centos(linux)下用命令导出mysql数据库数据
  9. pytorch torchvision.transform.Compose
  10. Java中单例模式—饿汉式和懒汉式
  11. 计算机视觉应用培训心得体会,三维计算机视觉学习感想
  12. Linux串口驱动分析及移植
  13. linux万能密码,Linux pam 后门纪录root用户密码以及设置万能密码登录root
  14. 计算机学院运动会加油,学校运会加油稿
  15. C/C++ FFmepeg Qt音视频开发录屏摄像机
  16. excel合并两列内容_Excel中如何跳过空单元格进行粘贴
  17. c++系列:关于MSVCR100.dll、MSVCR100d.dll、Msvcp100.dll、Msvcp100D.dll 故障查及解决方法
  18. Java编程题之某年某月某日
  19. 建模实训报告总结_模型实训心得体会
  20. 进程已结束,退出代码-1073740791 (0xC0000409)

热门文章

  1. hdu 1811Rank of Tetris (并查集 + 拓扑排序)
  2. java中对象的初始化过程
  3. jade软件_TEM衍射斑点标定之DM软件
  4. crt中 新建的连接存储在哪_数字存储示波器的VPO技术
  5. android studio日历小程序,android studio无法加载日历界面
  6. python stdin和stdout_stdin似乎比stdout(python)慢得多.为什么?
  7. Java集合:Set集合
  8. linux命令之有关网络的操作命令
  9. ansible的参数及常用模块
  10. 148. Sort List 1