ruby hash方法
哈希選擇方法 (Hash.select Method)
In this article, we will study about Hash.select 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.select方法 。 可以借助其名稱來預測此方法的工作,但是它并不像看起來那樣簡單。 好了,我們將在其余內容中借助其語法和程序代碼來理解此方法。
Method description:
方法說明:
This method is a public instance method that is defined in the ruby library especially for the Hash class. This method works in a way that it removes every key-value pair from the hash object for which the block has evaluated to be false. If you are not providing any block, then this method will return an enumerator.
此方法是在ruby庫中定義的公共實例方法,尤其是針對Hash類。 此方法的工作方式是,從該塊已評估為false的哈希對象中刪除每個鍵值對。 如果不提供任何塊,則此方法將返回一個枚舉器。
This method is one of the examples of?non-destructive methods?where changes created by the methods are non-permanent or temporary.
此方法是非破壞性方法的示例之一,其中,由這些方法創建的更改是非永久性或臨時性的。
Syntax:
句法:
Hash_object.select
or
Hash_object.select{|key,value| block}
Argument(s) required:
所需參數:
This method does not require any argument. You will need to pass a block with the method for its better implementation.
此方法不需要任何參數。 您將需要將該方法傳遞給一個塊,以實現更好的實現。
Example 1:
范例1:
=begin
Ruby program to demonstrate select method
=end
hash1={"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}
puts "Hash select implementation"
puts "Enter the key you want to keep:"
ky = gets.chomp
puts "Hash after select :#{hash1.select{|key,value| key==ky}}"
puts "Self hash object : #{hash1}"
Output
輸出量
Hash select implementation
Enter the key you want to keep:
color
Hash after select :{"color"=>"Black"}
Self hash object : {"color"=>"Black", "object"=>"car", "love"=>"friends", "fruit"=>"Kiwi", "vege"=>"potato"}
Explanation:
說明:
In the above code, you can observe that we are deleting elements from the hash object with the help of the Hash.select() method. You can see that the method is deleting all the elements for which the method has returned false. This method is one of the examples of non-destructive methods because it is not creating changes in the self hash object.
在上面的代碼中,您可以觀察到我們借助于Hash.select()方法從哈希對象中刪除元素。 您可以看到該方法正在刪除該方法返回false的所有元素。 此方法是非破壞性方法的示例之一,因為它沒有在自哈希對象中創建更改。
Example 2:
范例2:
=begin
Ruby program to demonstrate select method
=end
hash1={"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}
puts "Hash select implementation"
puts "Hash after select :#{hash1.select}"
puts "Self hash object : #{hash1}"
Output
輸出量
Hash select implementation
Hash after select :#<Enumerator:0x000055fd5496c880>
Self hash object : {"color"=>"Black", "object"=>"car", "love"=>"friends", "fruit"=>"Kiwi", "vege"=>"potato"}
Explanation:
說明:
In the above code, you can observe that this method returns an enumerator when called without providing any block at the time of invocation.
在上面的代碼中,您可以觀察到該方法在調用時返回一個枚舉器,而在調用時沒有提供任何塊。
翻譯自: https://www.includehelp.com/ruby/hash-select-method-with-example.aspx
ruby hash方法