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 the help of its syntax and program code in the rest of the content.

在本文中,我們將研究Hash.invert方法 。 可以借助其名稱來預測此方法的工作,但是它并不像看起來那樣簡單。 好了,我們將在其余內容中借助其語法和程序代碼來理解此方法。

Method description:

方法說明:

This method is a public instance method that is defined in Ruby library especially for Hash class. This method works in a way that it creates a new hash object in which the keys of the self hash are values and values of the self hash are keys. If a key with the same values already exists in the hash object then the last key will be utilized and the previous one will be discarded. This method is one of the examples of non-destructive methods, where the changes created by the method are temporary.

此方法是在Ruby庫中定義的公共實例方法,特別是針對Hash類。 此方法的工作方式是創建一個新的哈希對象,其中自哈希的鍵是值,而自哈希的值是鍵。 如果散列對象中已經存在具有相同值的鍵,則將使用最后一個鍵,而前一個鍵將被丟棄。 此方法是非破壞性方法的示例之一,其中該方法創建的更改是臨時的。

Syntax:

句法:

    Hash_object.invert

Argument(s) required:

所需參數:

This method does not require any argument.

此方法不需要任何參數。

Example 1:

范例1:

=begin
Ruby program to demonstrate invert method
=end	
hash1={"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}
puts "Hash.invert implementation"
ary = hash1.invert
puts "Hash object after inverting: #{ary}"
puts "Self hash object : #{hash1}"

Output

輸出量

Hash.invert implementation
Hash object after inverting: {"Black"=>"color", "car"=>"object", "friends"=>"love", "Kiwi"=>"fruit", "potato"=>"vege"}
Self hash object : {"color"=>"Black", "object"=>"car", "love"=>"friends", "fruit"=>"Kiwi", "vege"=>"potato"}

Explanation:

說明:

In the above code, you can observe that we are inverting the hash object with the help of the Hash.invert method. In the new hash, the keys are values and values are keys. You can see that this method is not creating any impact upon the actual hash because this method is one of the examples of non-destructive methods.

在上面的代碼中,您可以觀察到我們在Hash.invert方法的幫助下反轉了哈希對象。 在新的哈希中,鍵是值,而值是鍵。 您可以看到該方法不會對實際哈希產生任何影響,因為該方法是非破壞性方法的示例之一。

Example 2:

范例2:

=begin
Ruby program to demonstrate invert method
=end	
hash1={"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}
puts "Hash.invert implementation"
ary = hash1.invert.invert
puts "Hash object after inverting: #{ary}"
puts "Self hash object : #{hash1}"

Output

輸出量

Hash.invert implementation
Hash object after inverting: {"color"=>"Black", "object"=>"car", "love"=>"friends", "fruit"=>"Kiwi", "vege"=>"potato"}
Self hash object : {"color"=>"Black", "object"=>"car", "love"=>"friends", "fruit"=>"Kiwi", "vege"=>"potato"}

Explanation:

說明:

In the above code, you can observe that we can invert a hash object with the help of the Hash.invert method. You can even see that we can do cascading of methods and we are getting the same hash after invoking invert twice.

在上面的代碼中,您可以觀察到我們可以借助Hash.invert方法反轉哈希對象。 您甚至可以看到我們可以級聯方法,并且調用兩次invert后得到相同的哈希值。

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

ruby hash方法

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

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

相關文章

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

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

【Unity】Update()和FixedUpdate()

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

約束執行區域(CER)

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

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

python 抓取網頁鏈接Prerequisite: 先決條件: 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…

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

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

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

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

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 …

js \n直接顯示字符串_顯示N個字符的最短時間

js \n直接顯示字符串Problem statement: 問題陳述&#xff1a; You need to display N similar characters on a screen. You are allowed to do three types of operation each time. 您需要在屏幕上顯示N個相似的字符。 每次允許您執行三種類型的操作。 You can insert a c…

示例 Demo 工程和 API 參考鏈接

Camera Explorer&#xff1a;有關 Windows Phone8 中有關增強 Camera API 的使用。文章鏈接 Filter Effects&#xff1a;對拍攝的照片或者圖片庫中的照片應用 Nokia Imaging SDK 中的濾鏡。文章鏈接 Filter Explorer&#xff1a;演示了對新拍攝圖片或者現有圖片的編輯功能&…

三、標簽準備

所有操作均在anaconda中的自己配置的環境下進行 一、安裝labelimg 因為YOLO模型所需要的樣本標簽必須是txt類型&#xff0c;本人使用labelimg軟件進行對圖像進行打標簽操作。 pip install pycocotools-windows pip install pyqt5 pip install labelimg 通過labelimg命令打…

ubuntu 8.04安裝應用軟件Can't find X includes錯誤解決辦法

系統很小。應用軟件都的自己裝。 首先把 APT’s database is not updated. # apt-get update    # apt-get upgrade 再裝其它軟件。 make xconfigure 無法運行時&#xff1a; apt-get install qt3-dev-tools 編譯QVFB  是出現&#xff1a; 出現&#xff1a;C preproces…

leetcode 39. 組合總和 思考分析

目錄1、題目2、思考分析3、未經優化代碼4、剪枝優化1、題目 給定一個無重復元素的數組 candidates 和一個目標數 target &#xff0c;找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的數字可以無限制重復被選取。 2、思考分析 解空間樹寬度部分即數…