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

ruby hash方法

哈希紧凑方法 (Hash.compact Method)

In this article, we will study about Hash.compact 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.compact方法 。 可以借助其名称来预测此方法的工作,但是它并不像看起来那样简单。 好了,我们将在其余内容中借助其语法和程序代码来理解此方法。

Method description:

方法说明:

This method is a public instance method that is defined in Ruby's library especially for the Hash class. This method is one of the examples of non-destructive methods where the changes brought by the method are not permanent or temporary. These types of methods do not affect the self Hash instance. This method works in a way that removes all the keys from the hash which are containing nil values and returns a new Hash object which does not contain any nil value.

此方法是在Ruby的库中定义的公共实例方法,尤其是针对Hash类。 此方法是非破坏性方法的示例之一,其中该方法带来的更改不是永久性或临时性的。 这些类型的方法不会影响自哈希实例。 此方法的工作方式是从哈希中删除所有包含nil值的键,并返回一个不包含任何nil值的新Hash对象。

Syntax:

句法:

    Hash_object.compact

Argument(s) required:

所需参数:

This method does not take any argument.

此方法不带任何参数。

Example 1:

范例1:

=begin
  Ruby program to demonstrate compact method
=end	

hash1= {"color"=> "Black", "object"=>nil, "love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}

puts "Hash compact implementation"

puts "Elements after compact operation: #{hash1.compact}"
puts "Array elements are:"
puts "#{hash1}"

Output

输出量

Hash compact implementation
Elements after compact operation: {"color"=>"Black", "love"=>"mom", "fruit"=>"Kiwi", "vege"=>"potato"}
Array elements are:
{"color"=>"Black", "object"=>nil, "love"=>"mom", "fruit"=>"Kiwi", "vege"=>"potato"}

Explanation:

说明:

In the above code, you can observe that we are removing nil value keys from the hash object with the help of the Hash.compact method. This method is not bringing any changes in the actual hash instance because this method is an example of the non-destructive method and you can observe this by yourself when the program is printing the original hash on which the method has been invoked.

在上面的代码中,您可以观察到,借助于Hash.compact方法 ,我们正在从哈希对象中删除nil个值键。 该方法不会在实际的哈希实例中带来任何变化,因为该方法是非破坏性方法的一个示例,在程序打印调用了该方法的原始哈希时,您可以自己观察一下。

Example 2:

范例2:

=begin
  Ruby program to demonstrate compact method
=end	

hash1= {"color"=> [nil,nil,"black"], "object"=>nil, "love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}

puts "Hash compact implementation"

puts "Elements after compact operation: #{hash1.compact}"
puts "Array elements are:"
puts "#{hash1}"

Output

输出量

Hash compact implementation
Elements after compact operation: {"color"=>[nil, nil, "black"], "love"=>"mom", "fruit"=>"Kiwi", "vege"=>"potato"}
Array elements are:
{"color"=>[nil, nil, "black"], "object"=>nil, "love"=>"mom", "fruit"=>"Kiwi", "vege"=>"potato"}

Explanation:

说明:

In the above code, you can observe that the Hash.compact method does not work upon the hashes which have keys along with more than one value. The method is returning the original hash without removing the nil value keys.

在上面的代码中,您可以观察到Hash.compact方法不适用于具有键以及多个值的哈希值。 该方法将返回原始哈希,而不会删除nil值键。

翻译自: https://www.includehelp.com/ruby/hash-compact-method-with-example.aspx

ruby hash方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值