ruby 生成哈希值_哈希拒绝! Ruby中的示例方法

ruby 生成哈希值

哈希拒绝! 方法 (Hash.reject! Method)

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

Method description:

方法说明:

This method is a public instance method that is defined in the ruby library especially for the Hash class. The changes created by this method are permanent or non-temporary because this method is an example of destructive methods. This method works in a way that it will delete all the keys for which the block has been evaluated to be false. If you are not providing any block then an enumerator will be returned.

此方法是在ruby库中定义的公共实例方法,尤其是针对Hash类。 由于此方法是破坏性方法的示例,因此此方法创建的更改是永久的或非临时的。 此方法的工作方式是删除已被评估为false的所有键。 如果您不提供任何块,则将返回一个枚举器。

Syntax:

句法:

    Hash_object.reject!{|key,value| block}

Argument(s) required:

所需参数:

This method does not require any argument. However, a block can be passed for the desired result.

此方法不需要任何参数。 但是,可以传递一个块以获得所需的结果。

Example 1:

范例1:

=begin
  Ruby program to demonstrate reject! method
=end	

hsh = Hash.new()

hsh["color"] = "Black"
hsh["age"] = 20
hsh["school"] = "Angels' Academy Haridwar"
hsh["college"] = "Graphic Era University"

puts "Hash reject! implementation"

puts "#{hsh.reject!{|key,value| key>="school"}}"

puts "Hash contents are : #{hsh}"

Output

输出量

Hash reject! implementation
{"color"=>"Black", "age"=>20, "college"=>"Graphic Era University"}
Hash contents are : {"color"=>"Black", "age"=>20, "college"=>"Graphic Era University"}

Explanation:

说明:

In the above code, you can observe that we are removing keys from the hash object based on some condition. The method is returning a hash which is containing all those keys which stood false when the condition was tested on them. The method is creating permanent change on the hash object.

在上面的代码中,您可以观察到我们正在基于某种条件从哈希对象中删除键。 该方法将返回一个散列,其中包含所有在条件上进行过测试时都为假的键。 该方法在哈希对象上创建永久更改。

Example 2:

范例2:

=begin
  Ruby program to demonstrate reject! method
=end	

hsh = Hash.new()

hsh["color"] = "Black"
hsh["age"] = 20
hsh["school"] = "Angels' Academy Haridwar"
hsh["college"] = "Graphic Era University"

puts "Hash reject! implementation"

puts "#{hsh.reject!}"

puts "Hash contents are : #{hsh}"

Output

输出量

Hash reject! implementation
#<Enumerator:0x000055b6246cff40>
Hash contents are : {"color"=>"Black", "age"=>20, "school"=>"Angels' Academy Haridwar", "college"=>"Graphic Era University"}

Explanation:

说明:

In the above code, you can observe that when we are invoking the method without any block, an enumerator has been returned without creating any changes in the actual hash.

在上面的代码中,您可以观察到,当我们在不带任何块的情况下调用该方法时,将返回一个枚举数,而不会在实际哈希中创建任何更改。

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

ruby 生成哈希值

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值