ruby 生成哈希值
In the last article, we have seen how we can compare two hash objects with the help of <= operator? "<=" method is a public instance method defined in Ruby's library.
在上一篇文章中,我們看到了如何在<=運算符的幫助下比較兩個哈希對象 ? “ <=“方法是Ruby庫中定義的公共實例方法。
In this article, we will see the implementation of "==" operator. The working is pretty clear with the help of its name. It is not as simple as it seems. We will figure it out in the content of this article. We will understand it with the help of syntaxes and demonstrating program codes.
在本文中,我們將看到“ ==” operator的實現 。 借助其名稱,工作非常清晰。 它并不像看起來那么簡單。 我們將在本文的內容中找到答案。 我們將借助語法和演示程序代碼來理解它。
Method description:
方法說明:
This method is a public instance method that is defined in Ruby's library especially for Hash class. This method works in a way that it carries out a comparison between two different hashes and returns a Boolean value. The method returns true when the hash is equal to another hash and returns false if it is not equal to another Hash instance. 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類。 該方法的工作方式是在兩個不同的哈希值之間進行比較并返回一個布爾值。 當哈希等于另一個哈希時,該方法返回true;如果不等于另一個哈希實例,則返回false。 如果兩個散列包含相同數量的鍵,并且那里的鍵值應與另一個散列對象中的對應對象相同,則認為它們是相等的。
Syntax:
句法:
Hash == Hash_object -> true or false
Parameter(s) required:
所需參數:
This method does not require any argument.
此方法不需要任何參數。
Example 1:
范例1:
=begin
Ruby program to demonstrate == operator
=end
hash1 = {"color"=> "Black", "object"=>"phone", "love"=>"mom","fruit"=>"Kiwi","pete"=>"potato"}
hash2= {"color"=> "Black", "object"=>"phone", "love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}
if(hash1==hash2)
puts "hash1 is equal to hash2"
else
puts "hash1 is not equal to hash2"
end
Output
輸出量
hash1 is not equal to hash2
Explanation:
說明:
In the above code, you can simply observe that the method has returned false inside the if the condition that is because the message is printed as "hash1 is not equal to hash2". This happened because hash1 is not having all the elements which are present in hash2 or it is not equal to hash2.
在上面的代碼中,您可以簡單地觀察到,如果條件是因為消息被打印為“ hash1不等于hash2” ,則該方法在false內返回了false。 發生這種情況是因為hash1并不具有hash2中存在的所有元素,或者它不等于hash2。
Example 2:
范例2:
=begin
Ruby program to demonstrate == operator
=end
hash1= {"color"=> "Black", "object"=>"phone", "love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}
hash2= {"color"=> "Black", "object"=>"phone", "love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}
if(hash1==hash2)
puts "hash1 is equal to hash2"
else
puts "hash1 is not equal to hash2"
end
Output
輸出量
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。
翻譯自: https://www.includehelp.com/ruby/hash-equal-to-operator.aspx
ruby 生成哈希值