ruby hash方法
哈希平化方法 (Hash.flatten Method)
In this article, we will study about Hash.flatten 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.flatten方法 。 可以借助其名稱來預測此方法的工作,但是它并不像看起來那樣簡單。 好了,我們将在其餘内容中借助其文法和程式代碼來了解此方法。
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 returns an array object after flattening the whole hash object. This means that each key-value pair will be converted into an array object or you can say that will become an individual element of the Array object. This method is a non-destructive method which means that the changes created by this method would not affect the actual hash instance.
此方法是在Ruby庫中定義的公共執行個體方法,特别是針對Hash類。 此方法的工作方式是,在展平整個哈希對象之後傳回一個數組對象。 這意味着每個鍵值對将轉換為數組對象,或者可以說将成為數組對象的單個元素。 此方法是一種非破壞性方法,這意味着此方法建立的更改不會影響實際的哈希執行個體。
Syntax: 句法:Hash_object.flatten
or
Hash_object.flatten(level)
Argument(s) required: 所需參數: This method only accepts one argument and that argument is nothing but the level of flattening you want to do with the hash object.
此方法僅接受一個參數,該參數不過是您要對哈希對象進行的展平級别。
Example 1: 範例1:=begin
Ruby program to demonstrate flatten method
=end
hash1={"color"=>"Black","object"=>["car","phone"],"love"=>["mom","friends"],"fruit"=>"Kiwi","vege"=>"potato"}
puts "Hash.flatten implementation"
ary = hash1.flatten
puts "Hash object after flatten: #{ary}"
puts "Self hash object : #{hash1}"
Output 輸出量 Hash.flatten implementation
Hash object after flatten: ["color", "Black", "object", ["car", "phone"], "love", ["mom", "friends"], "fruit", "Kiwi", "vege", "potato"]
Self hash object : {"color"=>"Black", "object"=>["car", "phone"], "love"=>["mom", "friends"], "fruit"=>"Kiwi", "vege"=>"potato"}
Explanation: 說明: In the above code, you can observe that we are flattening the hash object with the help of the Hash.flatten method. The first level flattening has been done in which all the hash elements are now a part of an array. 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.flatten方法的幫助下将哈希對象展平。 已經完成了第一級扁平化,其中所有哈希元素現在都成為數組的一部分。 您可以看到該方法不會對實際哈希産生任何影響,因為該方法是非破壞性方法的示例之一。
Example 2: 範例2:=begin
Ruby program to demonstrate flatten method
=end
hash1={"color"=>"Black","object"=>["car","phone"],"love"=>["mom","friends"],"fruit"=>"Kiwi","vege"=>"potato"}
puts "Hash.flatten implementation"
puts "Enter the level of flatten"
lvl = gets.chomp.to_i
ary = hash1.flatten(lvl)
puts "Hash object after flatten: #{ary}"
puts "Self hash object : #{hash1}"
Output 輸出量 Hash.flatten implementation
Enter the level of flatten
2
Hash object after flatten: ["color", "Black", "object", "car", "phone", "love", "mom", "friends", "fruit", "Kiwi", "vege", "potato"]
Self hash object : {"color"=>"Black", "object"=>["car", "phone"], "love"=>["mom", "friends"], "fruit"=>"Kiwi", "vege"=>"potato"}
Explanation: 說明: In the above code, you can observe that we are flattening the hash object with the help of the Hash.flatten method. The second level flattening has been done in which all the hash elements are now a part of an array. 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.flatten方法的幫助下将哈希對象展平。 已經完成了第二級展平,其中所有哈希元素現在都成為數組的一部分。 您可以看到該方法不會對實際哈希産生任何影響,因為該方法是非破壞性方法的示例之一。
翻譯自: https://www.includehelp.com/ruby/hash-flatten-method-with-example.aspx
ruby hash方法