博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ruby hash方法_Ruby中带有示例的Hash.flatten方法
阅读量:2531 次
发布时间:2019-05-11

本文共 3641 字,大约阅读时间需要 12 分钟。

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.flattenputs "Hash object after flatten: #{ary}"puts "Self hash object : #{hash1}"

Output

输出量

Hash.flatten implementationHash 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_iary = hash1.flatten(lvl)puts "Hash object after flatten: #{ary}"puts "Self hash object : #{hash1}"

Output

输出量

Hash.flatten implementationEnter the level of flatten 2Hash 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方法的帮助下将哈希对象展平。 已经完成了第二级展平,其中所有哈希元素现在都成为数组的一部分。 您可以看到该方法不会对实际哈希产生任何影响,因为该方法是非破坏性方法的示例之一。

翻译自:

ruby hash方法

转载地址:http://zttzd.baihongyu.com/

你可能感兴趣的文章
VMware黑屏解决方法
查看>>
JS中各种跳转解析
查看>>
JAVA 基础 / 第八课:面向对象 / JAVA类的方法与实例方法
查看>>
Ecust OJ
查看>>
P3384 【模板】树链剖分
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
Equivalent Strings
查看>>
flume handler
查看>>
收藏其他博客园主写的代码,学习加自用。先表示感谢!!!
查看>>
H5 表单标签
查看>>
su 与 su - 区别
查看>>
C语言编程-9_4 字符统计
查看>>
在webconfig中写好连接后,在程序中如何调用?
查看>>
限制用户不能删除SharePoint列表中的条目(项目)
查看>>
【Linux网络编程】使用GDB调试程序
查看>>
feign调用spring clound eureka 注册中心服务
查看>>
ZT:Linux上安装JDK,最准确
查看>>
LimeJS指南3
查看>>