as_hash ruby_Ruby中带有示例的Hash.keep_if方法

as_hash ruby

Hash.keep_if方法 (Hash.keep_if Method)

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

Method description:

方法说明:

This method is a public instance method that is defined in the ruby library especially for Hash class. This method works in a way that it removes every key-value pair from the hash object for which the block has evaluated to be false. If you are not providing any block, then this method will return an enumerator.

此方法是在ruby库中定义的公共实例方法,特别是针对Hash类。 此方法的工作方式是,从该块已评估为false的哈希对象中删除每个键值对。 如果不提供任何块,则此方法将返回一个枚举器。

This method is one of the examples of destructive methods where changes created by the methods are permanent or non-temporary.

此方法是破坏性方法的示例之一,该方法所产生的更改是永久性的或非临时性的。

Syntax:

句法:

    Hash_object.keep_if
    or
    Hash_object.keep_if{|key,value| block}

Argument(s) required:

所需参数:

This method does not require any argument. You will need to pass a block with the method for its better implementation.

此方法不需要任何参数。 您将需要将该方法传递给一个块,以实现更好的实现。

Example 1:

范例1:

=begin
  Ruby program to demonstrate keep_if method
=end	

hash1={"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}

puts "Hash keep_if implementation"

puts "Enter the key you want to keep:"
ky = gets.chomp

puts "Hash after keep_if :#{hash1.keep_if{|key,value| key==ky}}"

puts "Self hash object : #{hash1}"

Output

输出量

Hash keep_if implementation
Enter the key you want to keep:
 color
Hash after keep_if :{"color"=>"Black"}
Self hash object : {"color"=>"Black"}

Explanation:

说明:

In the above code, you can observe that we are deleting elements from the hash object with the help of Hash.keep_if method. You can see that the method is deleting all the elements for which the method has returned false. This method is one of the examples of destructive methods because it is creating changes in the self hash objects.

在上面的代码中,您可以观察到我们正在借助Hash.keep_if方法从哈希对象中删除元素。 您可以看到该方法正在删除该方法返回false的所有元素。 此方法是破坏性方法的示例之一,因为它在自哈希对象中创建更改。

Example 2:

范例2:

=begin
  Ruby program to demonstrate keep_if method
=end	

hash1={"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}

puts "Hash keep_if implementation"

puts "Hash after keep_if :#{hash1.keep_if}"
puts "Self hash object : #{hash1}"

Output

输出量

Hash keep_if implementation
Hash after keep_if :#<Enumerator:0x000055f1998a3b90>
Self hash object : {"color"=>"Black", "object"=>"car", "love"=>"friends", "fruit"=>"Kiwi", "vege"=>"potato"}

Explanation:

说明:

In the above code, you can observe that this method returns an enumerator when called without providing any block at the time of invocation.

在上面的代码中,您可以观察到该方法在调用时返回一个枚举器,而在调用时没有提供任何块。

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

as_hash ruby

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值