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.mzph.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虚拟机.在这个系统中:的类加载器是负责正…