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

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

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

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

相關文章

浙江嘉興“網事”再添國字招牌 領跑城市智慧轉型

16日,舉世矚目的第三屆世界互聯網大會如期而至。世界目光再次聚焦“烏鎮時間”,人們不禁發現,流淌千年的水網已與迅猛發展的互聯網交相輝映,1300多年的古鎮釋放出強烈的互聯網信號。更令人驚喜的是,互聯網形成的沖擊波…

java list過濾重復的數據_List 去除重復數據的 5 種正確姿勢!

以下介紹五種-不同的方法去除 Java 中ArrayList中的重復數據1.使用LinkedHashSet刪除arraylist中的重復數據LinkedHashSet是在一個ArrayList刪除重復數據的最佳方法。LinkedHashSet在內部完成兩件事:刪除重復數據保持添加到其中的數據的順序Java示例使用LinkedHashS…

打擊侵犯公民個人信息罪的司法困境

當前,公民個人信息泄露并屢遭侵犯已成為社會關注焦點。泄露的信息輕則給被害人生活造成困擾,重則使被害人陷入電信詐騙、敲詐勒索等犯罪漩渦,造成重大人身、財產損失。一些民眾認為,對侵犯公民個人信息行為的刑事打擊很不給力&…

ruby中、.reject_Ruby中帶有示例的Array.reject方法

ruby中、.rejectRuby Array.reject方法 (Ruby Array.reject Method) In the last article, we have seen how we can make use of the Array.select method in order to print the Array elements based on certain conditions provided inside the block? In this article, w…

java獲取主機mac_Java 如何獲取主機的MAC地址

獲取MAC地址首先要理解當前的操作系統,由于在不同的操作系統中CMD命令所在的位置不同,因此首先使用System類中的getProperty("os.name")方法獲取當前的操作系統,getProperty()方法可以確定當前系統屬性,它的參數是一些固…

微軟免費軟件項目DreamSpark更名為Microsoft Imagine

9月10日消息,微軟免費軟件項目DreamSpark近日正式更名為Microsoft Imagine,將與一年一度的微軟“創新杯(Imagine Cup)”齊名。微軟免費軟件項目DreamSpark更名為Microsoft Imagine  2008年2月19日,微軟公司董事長比爾蓋茨在斯坦福大學發布了…

java jpa_Java JPA 語法知識

前提操作創建一個可持久化的實體類dao層繼承JpaRepositoryT:實體類ID:實體類的主鍵類型例:public interface SysUserRespository extends JpaRepository {}JPA中支持的關鍵詞And --- 等價于 SQL 中的 and 關鍵字,比如 findByUsern…

array.slice_Ruby中帶有示例的Array.slice()方法

array.sliceArray.slice()方法 (Array.slice() Method) In this article, we will study about Array.slice() method. You all must be thinking the method must be doing something which is related to the slicing of elements or objects in the Array instance. It is n…

阿特斯攜手EDF啟動建設巴西191.5MW光伏項目

2016年10月11日,阿特斯太陽能(安大略省,圭爾夫)和EDF Energies Nouvelles(法國,巴黎)共同宣布,將阿特斯巴西Pirapora I太陽能項目80%的股權出售給EDF的巴西本地子公司EDF…

apachejmeter_java源碼_自定義編寫jmeter的Java測試代碼

我們在做性能測試時,有時需要自己編寫測試腳本,很多測試工具都支持自定義編寫測試腳本,比如LoadRunner就有很多自定義腳本的協議,比如"C Vuser","JavaVuser"等協議.同樣,Jmeter也支持自定義編寫的測試代碼,不過與LoadRunner不同的是,Jmeter沒有自帶編譯器,…

julia fit 函數_帶有Julia中示例的flipsign()函數

julia fit 函數Julia| flipsign()函數 (Julia | flipsign() function) flipsign() function is a library function in Julia programming language, it accepts two values as parameters and returns a value with the magnitude of first value and sign of the first value…

優化Android應用內存的若干方法

https://my.oschina.net/chaselinfo/blog/198172摘要: 在app開發的各個階段中要考慮RAM的限制問題, 包括在設計階段(正式開發之前). 使用下面的不同的方法可以達到很好的效果. 當您在設計和開發Android應用時用下面的方法可以使內存運用最高效.使用保守的Service 如果你的應用需…

一? ilkkn.n_IL&FS的完整形式是什么?

一? il&kkn.nIL&FS:基礎設施租賃和金融服務 (IL& FS: Infrastructure Leasing & Financial Services) IL&FS is an abbreviation of Infrastructure Leasing & Financial Services. It is the largest infrastructure development …

java notify喚醒原理_Java wait和notify虛假喚醒原理

自己在此記錄一下,方便日后復習。虛假喚醒的概念jdk官方文檔解釋:所以說在wait和notify一塊使用時,如果使用if作為條件時,會有虛假喚醒的情況發生,所以必須使用while作為循環條件。下面來舉例實驗:首先&…

C#里面的三種定時計時器:Timer

在.NET中有三種計時器:1、System.Windows.Forms命名空間下的Timer控件,它直接繼承自Componet。Timer控件只有綁定了Tick事件和設置EnabledTrue后才會自動計時,停止計時可以用Stop()方法控制,通過Stop()停止之后,如果想…

wireshark rto_RTO的完整形式是什么?

wireshark rtoRTO:地區運輸辦公室/公路運輸辦公室 (RTO: Regional Transport Office/ Road Transport Office) RTO is an abbreviation of the Regional Transport Office. It is an Indian Government departmental organization that is responsible for upholdin…

java8 json轉xml_2019-08-17java對象與json,xml互轉

依賴的jar包,jackson-all-1.7.6.jar,xstream-1.4.4.jar下載地址:鏈接:https://pan.baidu.com/s/1LflD135qlQiIPGXw5XwDmw提取碼:6v29復制這段內容后打開百度網盤手機App,操作更方便哦package json_xml;import com.thoughtworks.xs…

10.8-全棧Java筆記:序列化/反序列化的步驟和實例

本節我們詳細講解10.3節中提到的序列化和反序列化操作。序列化和反序列化是什么當兩個進程遠程通信時,彼此可以發送各種類型的數據。 無論是何種類型的數據,都會以二進制序列的形式在網絡上傳送。比如,我們可以通過http協議發送字符串信息&am…

有效的網絡推廣超級實用方法

我叫龍雨,先后在百度搜狗工作過3年,后來一直負責一家公司的的網絡營銷!不知道大家有沒有聽過111>3這樣一個概念,簡單來說一下這概念!第一呢就是自己的資源,把自己的資源維護好開發好;第二就是網絡營銷,網絡營銷利用…

什么為java運行時的環境_什么是JRE?Java運行時環境簡介(一)

Java開發工具包(JDK),Java虛擬機(JVM)和Java運行時環境(JRE)共同構成了用于開發和運行Java應用程序的Java平臺組件的強大功能.實際上,運行時環境是一種旨在運行其他軟件的軟件.作為Java的運行時環境,JRE包含Java類庫,Java類加載器和Java虛擬機.在這個系統中:的類加載器是負責正…