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

ruby hash方法_Ruby中带有示例的Hash.flatten方法相关推荐

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

    ruby hash方法 Hash.invert方法 (Hash.invert Method) In this article, we will study about Hash.invert Meth ...

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

    ruby hash方法 哈希选择方法 (Hash.select Method) In this article, we will study about Hash.select Method. The ...

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

    ruby hash方法 Hash.rehash方法 (Hash.rehash Method) In this article, we will study about Hash.rehash Meth ...

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

    ruby hash方法 哈希值方法 (Hash.values Method) In this article, we will study about Hash.values Method. The ...

  5. ruby hash方法_Ruby中带有示例的Hash.default(key = nil)方法

    ruby hash方法 Hash.default(key = nil)方法 (Hash.default(key=nil) Method) In this article, we will study ...

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

    ruby hash方法 哈希长度方法 (Hash.length Method) In this article, we will study about Hash.length Method. The ...

  7. ruby hash方法_Ruby中带有示例的Hash.key?(obj)方法

    ruby hash方法 Hash.key?(obj)方法 (Hash.key?(obj) Method) In this article, we will study about Hash.key?( ...

  8. ruby hash方法_Ruby中带有示例的Hash.rassoc(obj)方法

    ruby hash方法 Hash.rassoc(obj)方法 (Hash.rassoc(obj) Method) In this article, we will study about Hash.r ...

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

    ruby hash方法 哈希键方法 (Hash.keys Method) In this article, we will study about Hash.keys Method. The work ...

最新文章

  1. bootcmd 和 bootargs 环境变量
  2. leetcode - Same Tree
  3. 精美技术图赏|技术精华
  4. 蓝桥杯 平面切分(欧拉定理)
  5. 开机自启动脚本_使用xtu降低笔记本(游戏本)cpu电压及功耗·游戏本延时(以及试着解决开机自启动的问题)...
  6. 信息学奥赛一本通 2054:【例3.4】适合晨练
  7. “锤死挣扎”的骁龙845+128GB旗舰机暴降1500元 仍冷清!
  8. ubuntu安装eclipse java环境变量配置_【Linux学习】Ubuntu下安装JDK以及配置环境,和eclipse的下载...
  9. Failed to decrypt protected XML node DTS:Property with error 0x8009000B 错误的解决
  10. mysql数据库隔离级别 实现原理_MySQL事务及Spring隔离级别实现原理详解
  11. python监控桌面捕捉,用Python从屏幕上捕获视频数据
  12. 基于Flink CDC实现实时数据采集(一)-接口设计
  13. 【python爬虫】《中华诗词大会》诗词接龙代码实现
  14. python爬虫爬取图片代码_python爬虫爬取图片的简单代码
  15. C语言的加减乘除函数
  16. web前端设计与开发期末作品/期末大作业:我的家乡——走进达州(4页) HTML+CSS+JavaScript
  17. Android 360全景视频播放。
  18. TB-RK3399Pro 找不到网络图标,如何连wifi呢?
  19. 韩信要是听了此人的建议,或许就不会死在这位女人手中了
  20. 外贸收款方式精辟分析

热门文章

  1. 云主机用linux还是winows,云服务器一般使用什么系统?Linux还是Windows?
  2. arm体系结构与编程_教程:如何学习嵌入式系统(基于ARM平台)
  3. C++中智能指针的原理、使用、实现
  4. php 做更新进度条,PHP exec()后更新Bootstrap进度条
  5. xampp mysql 卸载_卸载Xampp并安装apache + mysql + php 过程
  6. Linux下Java的安装与配置
  7. P1040 加分二叉树【dp+深搜】
  8. PostgreSQL 并行查询概述
  9. return ,continue,break的用法与区别总结
  10. 存储芯片在智能化产业链中扮演的角色将更加重要