ruby hash方法_Ruby中帶有示例的Hash.select方法

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方法

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377878.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377878.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377878.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

leetcode 77. 組合 思考分析

目錄1、題目2、回溯法思路3、參考其他思路&#xff0c;更深入了解這個問題4、剪枝優化可能需要回顧到的知識文章&#xff1a;1、常用算法總結(窮舉法、貪心算法、遞歸與分治算法、回溯算法、數值概率算法)2、回溯法初步刪除vector容器中的對象元素的三種方法:pop_back, erase與…

ASP 調用dll(VB)及封裝dll實例

ASP調用dll及封裝dll實例&#xff0c;封裝為dll可以提供運行效率&#xff0c;加密代碼。 打開VB6&#xff0c;新建ActiveX DLL 2、在工程引用中加入Microsoft Active Server Pages Object Library選擇 3、填加代碼如下Code Start 聲明部分 Private MyScriptingContext As Scrip…

三、全景拼接

一、項目所涉及到的一些知識點 Ⅰ&#xff0c;BF(Brute-Force)暴力匹配&#xff1a;把兩張圖像的特征點全部給算出來&#xff0c;然后使用歸一化的歐氏距離比較這兩張圖像上特征點之間的大小關系&#xff0c;越小越相似。 SIFT算法 import cv2 import numpy as np import ma…

找回自建SVN密碼

自建了一個SVN Repo自己用。重裝系統之后密碼忘了。 經過了漫長的Google過程&#xff0c;才知道Repo中的密碼居然是明文保存的。 在yourRepoDir/conf/svnserve.conf下的password-db處設置&#xff0c;通常是yourRepoDir/conf/passwd文件。 打開passwd文件&#xff0c;就是明文保…

ruby hash方法_Ruby中帶有示例的Hash.invert方法

ruby hash方法Hash.invert方法 (Hash.invert Method) In this article, we will study about Hash.invert 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 …

leetcode 216. 組合總和 III 思考分析

可能需要回顧的文章; leetcode 77. 組合 思考分析 1、題目 找出所有相加之和為 n 的 k 個數的組合。組合中只允許含有 1 - 9 的正整數&#xff0c;并且每種組合中不存在重復的數字。 說明&#xff1a; 所有數字都是正整數。 解集不能包含重復的組合。 2、遞歸 這一題和之前…

【Unity】Update()和FixedUpdate()

Update()每幀調用&#xff0c;FixedUpdate&#xff08;&#xff09;以指定頻率被調用。可以在 Edit -> project settings -> Time -> Fixed Timestep 中設定該頻率。轉載于:https://www.cnblogs.com/xiayong123/p/3717002.html

約束執行區域(CER)

受約束的執行區域 (CER) 是創作可靠托管代碼的機制的一部分。CER 定義一個區域&#xff0c;在該區域中公共語言運行庫 (CLR) 會受到約束&#xff0c;不能引發可使區域中的代碼無法完全執行的帶外異常。在該區域中&#xff0c;用戶代碼受到約束&#xff0c;不能執行會導致引發帶…

python 抓取網頁鏈接_從Python中的網頁抓取鏈接

python 抓取網頁鏈接Prerequisite: 先決條件&#xff1a; Urllib3: It is a powerful, sanity-friendly HTTP client for Python with having many features like thread safety, client-side SSL/TSL verification, connection pooling, file uploading with multipart encod…

四、模擬英語四六級答題卡識別閱卷評分

一、思路分析 首先拿到答題卡照片的時候&#xff0c;需要對照片進行一系列預處理操作&#xff0c;通過透視變換將圖像擺正方便后續的操作。每一道題五個選項&#xff0c;有五道題&#xff0c;通過字典存放準確答案。沒有依次對答題卡進行輪廓檢測&#xff0c;這里采用的是正方…

leetcode 17. 電話號碼的字母組合 思考分析

題目 給定一個僅包含數字 2-9 的字符串&#xff0c;返回所有它能表示的字母組合。 給出數字到字母的映射如下&#xff08;與電話按鍵相同&#xff09;。注意 1 不對應任何字母。 思考與遞歸程序 解空間樹的寬度是輸入數字對應的字符的個數&#xff0c;深度是輸入的數字的個數…

Blockquotes,引用,html里面,經常用到的一個!

blockquote元素的使用已經非常多樣化&#xff0c;但語義上它只適用于一件事–標記了一段你的網頁被引用從另一來源。這意味著&#xff0c;如果你想讓那些花俏的引文&#xff0c;<blockquote>是不是你應該使用元素。讓我們看一看如何你應該使用此元素&#xff1a; <art…

仔細分析了下這7行,貌似時間復雜度,空間復雜度都不大,為嘛就是執行效率這么低?...

for(Girl girl Girls.first(); !myGirlFriend.like(me); girl Girls.next()){if(!girl.hasBoyFriend(now) && i.like(girl)) { GirlFriend myGirlFriend (GirlFriend)girl; }} 轉載于:https://www.cnblogs.com/naran/archive/2011/12/28/2305467.html…

BHMS的完整形式是什么?

BHMS&#xff1a;順勢療法醫學和外科學士 (BHMS: Bachelor of Homeopathic Medicine and Surgery) BHMS is an abbreviation of Bachelor of Homeopathic Medicine and Surgery. It is a medical degree program for under graduation in Homeopathy; an alternative move towa…

c++編程思想2 --友元存儲控制

友元friend在c中的應用 我們知道在c的類訪問權限中,private和 protected在類外面進行訪問的時候 會因為權限而不能訪問 &#xff0c;友元就解決了這個問題 。 可以這樣理解&#xff0c;他為外部的 函數 或者類 進行了 訪問授權,其實這已經超出OOP的范疇,但是對于C而言是以實用…

WordPress Event Easy Calendar插件多個跨站請求偽造漏洞

漏洞名稱&#xff1a;WordPress Event Easy Calendar插件多個跨站請求偽造漏洞CNNVD編號&#xff1a;CNNVD-201309-083發布時間&#xff1a;2013-09-11更新時間&#xff1a;2013-09-11危害等級&#xff1a; 漏洞類型&#xff1a;跨站請求偽造威脅類型&#xff1a;遠程CVE編號&…

XML轉txt格式腳本

一、東北大學老師收集的鋼材缺陷數據集是XML格式的&#xff0c;但是YOLOv5只允許使用txt文件標簽 例如其中一種缺陷圖片所對應的標簽&#xff1a;crazing_1.xml <annotation><folder>cr</folder><filename>crazing_1.jpg</filename><source&…

python程序生成exe_使用Python程序生成QR代碼的Python程序

python程序生成exeQR code is a short form of the quick response code. It is a type of matrix barcode that contains some information like some specific link, important message, email-id, etc. In Python, the qrcode module is used to generate the QR code of so…

leetcode 242. 有效的字母異位詞 思考分析

題目 給定兩個字符串 s 和 t &#xff0c;編寫一個函數來判斷 t 是否是 s 的字母異位詞。 我們先考慮低階版本&#xff0c;認為字符只有26種可能&#xff0c;然后將a ~ z的字符映射到數組的索引0 ~ 25&#xff0c;數組中存放的則是該索引出現的頻次。 記錄下s的頻次和t的頻次…

總結一下ERP .NET程序員必須掌握的.NET技術,掌握了這些技術工作起來才得心應手...

從畢業做.NET到現在&#xff0c;有好幾年了&#xff0c;自認為只能是達到熟練的水平&#xff0c;談不上精通。所以&#xff0c;總結一下&#xff0c;自己到底熟練掌握了哪些.NET方面的開發技術&#xff0c;以此對照&#xff0c;看看還有哪些不足&#xff0c;歡迎補充。 1 .NET …