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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/1 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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:

=beginRuby program to demonstrate invert method =end hash1={"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}puts "Hash.invert implementation" ary = hash1.invertputs "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:

=beginRuby program to demonstrate invert method =end hash1={"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}puts "Hash.invert implementation" ary = hash1.invert.invertputs "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方法

總結

以上是生活随笔為你收集整理的ruby hash方法_Ruby中带有示例的Hash.invert方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。