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

ruby hash方法

Hash.eql?()方法 (Hash.eql?() Method)

In this article, we will study about Hash.eql?() Method. The working of this method can be predicted with the help of its name but it is not as simple as it seems. Well, we will understand this method with the help of its syntax and program code in the rest of the content.

在本文中,我们将研究Hash.eql?()方法 。 可以借助其名称来预测此方法的工作,但是它并不像看起来那样简单。 好了,我们将在其余内容中借助其语法和程序代码来理解此方法。

Method description:

方法说明:

This method is a public instance method that is defined in the ruby library especially for Hash class. This method works in a way that it carries out a comparison between two hashes. The first hash will be the hash object on which this method has been invoked and the second hash object will be the hash object which has been passed with the method. Hash.eql?() method returns a Boolean value and it will return true if both the hashes are equal and will return false if they both are not. The order of each hash is not compared. Two hashes are considered to be equal if they contain the same number of keys and there key values should be the same as the corresponding object in another hash object.

此方法是在ruby库中定义的公共实例方法,特别是针对Hash类。 此方法的工作方式是在两个哈希之间进行比较。 第一个哈希将是已在其上调用此方法的哈希对象,第二个哈希将是已随该方法传递的哈希对象。 Hash.eql?()方法返回一个布尔值,如果两个哈希值相等,则返回true;如果两个哈希值都不相同,则返回false。 不比较每个哈希的顺序。 如果两个散列包含相同数量的键,并且那里的键值应与另一个散列对象中的对应对象相同,则认为它们是相等的。

Syntax:

句法:

    Hash_object.eql?(hash_object1)

Argument(s) required:

所需参数:

This method only takes a single argument. The hash object passed is the object which will be compared with the first one.

此方法仅接受一个参数。 传递的哈希对象是将与第一个对象进行比较的对象。

Example 1:

范例1:

=begin
  Ruby program to demonstrate .eql? operator
=end	

hash1={"color"=>"Black","object"=>"phone","love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}
hash2={"color"=>"Black","object"=>"phone","love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}

puts "Hash.eql? implementation"

if(hash1.eql?(hash2))
	puts "hash1 is equal to hash2"
else
	puts "hash1 is not equal to hash2"
end

Output

输出量

Hash.eql? implementation
hash1 is equal to hash2

Explanation:

说明:

In the above code, you can simply observe that the method has returned true inside the if the condition that is because the message is printed as "hash1 is equal to hash2". This happened because hash1 is having all the elements which are present in hash2 or it is equal to hash2.

在上面的代码中,您可以简单地观察到,如果条件是因为消息被打印为“ hash1等于hash2” ,则该方法在true内部返回了true。 发生这种情况是因为hash1具有hash2中存在的所有元素,或者它等于hash2。

Example 2:

范例2:

=begin
  Ruby program to demonstrate .eql? operator
=end	

hash1={"color"=>"Black","object"=>"phone","love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}
hash2={"object"=>"phone","fruit"=>"Kiwi","vege"=>"potato","love"=>"mom","color"=> "Black"}

puts "Hash.eql? implementation"

if(hash1.eql?(hash2))
	puts "hash1 is equal to hash2"
else
	puts "hash1 is not equal to hash2"
end

Output

输出量

Hash.eql? implementation
hash1 is equal to hash2

Explanation:

说明:

In the above code, you can observe that this method does not put emphasis upon the order of each hash object. It simply means that no matter in which order elements are stored in both the hashes, if the elements are the same then two hashes are equal otherwise not.

在上面的代码中,您可以观察到该方法没有强调每个哈希对象的顺序。 它只是意味着无论元素在两个哈希中存储的顺序如何,如果元素相同,则两个哈希相等,否则不相等。

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

ruby hash方法

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值