as_hash ruby_Ruby中带有示例的Hash.keep_if方法
as_hash ruby
Hash.keep_if方法 (Hash.keep_if Method)
In this article, we will study about Hash.keep_if 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.keep_if方法 。 可以借助其名稱來預(yù)測此方法的工作,但是它并不像看起來那樣簡單。 好了,我們將在其余內(nèi)容中借助其語法和程序代碼來理解此方法。
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 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 destructive methods where changes created by the methods are permanent or non-temporary.
此方法是破壞性方法的示例之一,該方法所產(chǎn)生的更改是永久性的或非臨時性的。
Syntax:
句法:
Hash_object.keep_iforHash_object.keep_if{|key,value| block}Argument(s) required:
所需參數(shù):
This method does not require any argument. You will need to pass a block with the method for its better implementation.
此方法不需要任何參數(shù)。 您將需要將該方法傳遞給一個塊,以實現(xiàn)更好的實現(xiàn)。
Example 1:
范例1:
=beginRuby program to demonstrate keep_if method =end hash1={"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}puts "Hash keep_if implementation"puts "Enter the key you want to keep:" ky = gets.chompputs "Hash after keep_if :#{hash1.keep_if{|key,value| key==ky}}"puts "Self hash object : #{hash1}"Output
輸出量
Hash keep_if implementation Enter the key you want to keep:color Hash after keep_if :{"color"=>"Black"} Self hash object : {"color"=>"Black"}Explanation:
說明:
In the above code, you can observe that we are deleting elements from the hash object with the help of Hash.keep_if 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 destructive methods because it is creating changes in the self hash objects.
在上面的代碼中,您可以觀察到我們正在借助Hash.keep_if方法從哈希對象中刪除元素。 您可以看到該方法正在刪除該方法返回false的所有元素。 此方法是破壞性方法的示例之一,因為它在自哈希對象中創(chuàng)建更改。
Example 2:
范例2:
=beginRuby program to demonstrate keep_if method =end hash1={"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}puts "Hash keep_if implementation"puts "Hash after keep_if :#{hash1.keep_if}" puts "Self hash object : #{hash1}"Output
輸出量
Hash keep_if implementation Hash after keep_if :#<Enumerator:0x000055f1998a3b90> 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.
在上面的代碼中,您可以觀察到該方法在調(diào)用時返回一個枚舉器,而在調(diào)用時沒有提供任何塊。
翻譯自: https://www.includehelp.com/ruby/hash-keep_if-method-with-example.aspx
as_hash ruby
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的as_hash ruby_Ruby中带有示例的Hash.keep_if方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python信号量怎么用_Python3
- 下一篇: IRCTC的完整形式是什么?