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

ruby hash方法

Hash.rassoc(obj)方法 (Hash.rassoc(obj) Method)

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

Method description:

方法说明:

This method is a Public instance method and belongs to the Hash class which lives inside the library of Ruby language. The Hash.rassoc() method is used to check whether a value(key-value) is a part of the particular Hash instance or not and that Hash instance should be the normal Hash instance. An abnormal means that Hash instance is the Hash of multiple Array instances along with 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.

此方法是Public实例方法,属于Hash类,它位于Ruby语言库中。 Hash.rassoc()方法用于检查值(键值)是否为特定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 value in Key-value pair. It will return "nil" if it hadn't found the value in any of the Hashes.

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

Syntax:

句法:

    Hash_instance.rassoc(obj)

Argument(s) required:

所需参数:

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

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

Example 1:

范例1:

=begin
  Ruby program to demonstrate Hash.rassoc method
=end	

hsh = {"colors"  => "red",
     "letters" => "a", "Fruit" => "Grapes"}

puts "Hash.rassoc implementation:"

puts "Enter the Value you want to search:-"
ky = gets.chomp

if (hsh.rassoc(ky))
	puts "Value found successfully"
	puts "Values are: #{hsh.rassoc(ky)}"
else
	puts "Value not found!"
end

Output

输出量

Hash.rassoc implementation:
Enter the Value you want to search:-
 blue
Value not found!

Explanation:

说明:

In the above code, you can find that the Hash instance on which we have invoked the Hash.rassoc() method is a normal Hash instance. It is not the collection of multiple Array instances along with their specific keys. It is returning the whole Array instance with the key where it has found the value inputted by the user.

在上面的代码中,您可以发现我们在其上调用Hash.rassoc()方法的Hash实例是普通的Hash实例。 它不是多个Array实例及其特定键的集合。 它返回整个Array实例,并返回找到用户输入值的键。

Example 2:

范例2:

=begin
  Ruby program to demonstrate Hash.rassoc method
=end	

hsh = {"color"=> ["green","blue","yellow"],"vege"=> ["papaya","brinjal"]}

puts "Hash rassoc implementation:"

puts hsh.rassoc("green")

Output

输出量

Hash rassoc implementation:

Explanation:

说明:

In the above code, you can verify that the Hash.rassoc() method does not work upon abnormal Hash instances. It will return nil even if the value is present in the Hash.

在上面的代码中,您可以验证Hash.rassoc()方法不适用于异常的Hash实例。 即使哈希中存在该值,它也将返回nil。

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

ruby hash方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值