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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值