日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

as_hash ruby_Ruby中带有示例的Hash.delete_if方法

發(fā)布時(shí)間:2023/12/1 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 as_hash ruby_Ruby中带有示例的Hash.delete_if方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

as_hash ruby

Hash.delete_if方法 (Hash.delete_if Method)

In this article, we will study about Hash.delete_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.delete_if方法 。 可以借助其名稱來預(yù)測(cè)此方法的工作,但是它并不像看起來那樣簡(jiǎn)單。 好了,我們將在其余內(nèi)容中借助其語(yǔ)法和程序代碼來理解此方法。

Method description:

方法說明:

This method is a public instance method that is defined in the ruby library especially for Hash class. The changes created by this method are permanent or non-temporary. This method works in a way that it will delete all the keys for which the block has been evaluated to be true. If you are not providing any block then an enumerator will be returned.

此方法是在ruby庫(kù)中定義的公共實(shí)例方法,特別是針對(duì)Hash類。 通過此方法創(chuàng)建的更改是永久的或非臨時(shí)的。 此方法以一種方式刪除所有已被評(píng)估為真的鍵。 如果您不提供任何塊,則將返回一個(gè)枚舉器。

Syntax:

句法:

Hash_object.delete_if{|key,value| block}

Argument(s) required:

所需參數(shù):

This method does not require any argument. However, a block can be passed for the desired result.

此方法不需要任何參數(shù)。 但是,可以傳遞一個(gè)塊以獲得所需的結(jié)果。

Example 1:

范例1:

=beginRuby program to demonstrate delete_if method =end hsh = Hash.new()hsh["color"] = "Black" hsh["age"] = 20 hsh["school"] = "Angels' Academy Haridwar" hsh["college"] = "Graphic Era University"puts "Hash delete_if implementation" puts "#{hsh.delete_if{|key,value| key>="school"}}" puts "Hash contents are : #{hsh}"

Output

輸出量

Hash delete_if implementation {"color"=>"Black", "age"=>20, "college"=>"Graphic Era University"} Hash contents are : {"color"=>"Black", "age"=>20, "college"=>"Graphic Era University"}

Explanation:

說明:

In the above code, you can observe that we are removing keys from the hash object based on some condition. The method is returning a hash which is containing all those keys which stood false when the condition was tested on them. The method is creating permanent change on the hash object.

在上面的代碼中,您可以觀察到我們正在基于某種條件從哈希對(duì)象中刪除鍵。 該方法將返回一個(gè)散列,其中包含所有在條件上進(jìn)行過測(cè)試時(shí)都為假的鍵。 該方法在哈希對(duì)象上創(chuàng)建永久更改。

Example 2:

范例2:

=beginRuby program to demonstrate delete_if method =end hsh = Hash.new()hsh["color"] = "Black" hsh["age"] = 20 hsh["school"] = "Angels' Academy Haridwar" hsh["college"] = "Graphic Era University"puts "Hash delete_if implementation" puts "#{hsh.delete_if}" puts "Hash contents are : #{hsh}"

Output

輸出量

Hash delete_if implementation #<Enumerator:0x00005654a524a428> Hash contents are : {"color"=>"Black", "age"=>20, "school"=>"Angels' Academy Haridwar", "college"=>"Graphic Era University"}

Explanation:

說明:

In the above code, you can observe that when we are invoking the method without any block, an enumerator has been returned without creating any changes in the actual hash.

在上面的代碼中,您可以觀察到,當(dāng)我們?cè)诓粠魏螇K的情況下調(diào)用該方法時(shí),將返回一個(gè)枚舉數(shù),而不會(huì)在實(shí)際哈希中創(chuàng)建任何更改。

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

as_hash ruby

總結(jié)

以上是生活随笔為你收集整理的as_hash ruby_Ruby中带有示例的Hash.delete_if方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。