ruby 生成哈希值
In the last article, we have seen how we can carry out a comparison between 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 the ">" 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.
在本文中,我們將看到“>”運算符的實現 。 借助其名稱,工作非常清晰。 它并不像看起來那么簡單。 我們將在本文的內容中找到答案。 我們將借助語法和演示程序代碼來理解它。
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 second hash is a subset of first hash and returns false if it is not the subset of the first Hash instance. Being a subset simply means to have all those elements which are present in another Hash object.
此方法是在Ruby的庫中定義的公共實例方法,特別是針對Hash類。 該方法的工作方式是在兩個不同的哈希值之間進行比較并返回一個布爾值。 當第二個哈希是第一個哈希的子集時,該方法返回true;如果它不是第一個Hash實例的子集,則返回false。 作為子集僅意味著擁有所有存在于另一個Hash對象中的所有元素。
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","vege"=>"potato","place"=>"null"}
hash2= {"color"=> "Black", "object"=>"phone", "love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}
if(hash1>hash2)
puts "hash2 is a subset of hash1"
else
puts "hash2 is not a subset of hash1"
end
Output
輸出量
hash2 is a subset of hash1
Explanation:
說明:
In the above code, you can simply observe that the method has returned true inside the if condition that is because the message is printed as "hash2 is the subset of? hash1". This happened because hash2 has all the elements which are present in hash1. This is the simple meaning of subset.
在上面的代碼中,您可以簡單地觀察到該方法已在if條件內返回true,這是因為消息被打印為“ hash2是hash1的子集” 。 發生這種情況是因為hash2具有hash1中存在的所有元素。 這是子集的簡單含義。
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","place"=>"null"}
if(hash1>hash2)
puts "hash2 is a subset of hash1"
else
puts "hash2 is not a subset of hash1"
end
Output
輸出量
hash2 is not a subset of hash1
Explanation:
說明:
In the above code, you can simply observe that the method has returned false inside the if condition that is because the message is printed as "hash2 is not a subset of hash1". This happened because hash2 is not having all the elements which are present in hash1. This is the simple meaning of subset.
在上面的代碼中,您可以簡單地觀察到該方法在if條件內返回了false,這是因為消息被打印為“ hash2不是hash1的子集” 。 發生這種情況是因為hash2并不具有hash1中存在的所有元素。 這是子集的簡單含義。
翻譯自: https://www.includehelp.com/ruby/hash-greater-than-operator.aspx
ruby 生成哈希值