ruby hash方法_Hash.delete()方法以及Ruby中的示例

ruby hash方法

Hash.delete()方法 (Hash.delete() Method)

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

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 deletes the key-value pair of the key which is passed with the method at the time of invocation. If the key is not found, it returns 'nil'. To avoid the 'nil' value, you can pass a block along with the method then the result of that value will be returned in case the key is not found.

此方法是在ruby库中定义的公共实例方法,特别是针对Hash类。 此方法的工作方式是删除调用时与该方法一起传递的键的键-值对。 如果未找到密钥,则返回'nil' 。 为了避免'nil'值,您可以将一个块与该方法一起传递,然后在找不到键的情况下返回该值的结果。

Syntax:

句法:

    Hash_object.delete(object)
    or
    Hash_object.delete(object){block}

Argument(s) required:

所需参数:

You can pass at most one object along with this method. An optional block can be given in order to avoid 'nil' value return.

您最多可以使用此方法传递一个对象。 为了避免返回“ nil”值,可以给出一个可选的块。

Example 1:

范例1:

=begin
  Ruby program to demonstrate delete method
=end	

hsh = Hash.new()

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

puts "Hash delete implementation"

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

if(hsh.delete(ky))
puts "Key deleted successfully"
else
	puts "Key not found"
end

puts "Hash contents are : #{hsh}"

Output

输出量

Hash delete implementation
Enter the key you want to delete:
 school
Key deleted successfully
Hash contents are : {"color"=>"Black", "age"=>20, "college"=>"Graphic Era University"}

Explanation:

说明:

In the above code, you can observe that the method is creating permanent changes on the hash object and the key along with its value has been deleted from the hash object.

在上面的代码中,您可以观察到该方法正在哈希对象上创建永久更改,并且键及其值已从哈希对象中删除。

Example 2:

范例2:

=begin
  Ruby program to demonstrate delete method
=end	

hsh = Hash.new()

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

puts "Hash delete implementation"

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

puts "#{hsh.delete(ky){|ky|"#{ky} not found"}}"

puts "Hash contents are : #{hsh}"

Output

输出量

Hash delete implementation
Enter the key you want to delete:
 car
car not found
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 the key is not found in the hash object then the block has been executed and the block processed value has been returned. This is possible with the help of the block otherwise 'nil' value must have been returned from the delete() method.

在上面的代码中,您可以观察到,当在哈希对象中找不到该键时,该块便已执行,并且该块的处理值已返回。 这可以借助块来实现,否则必须从delete()方法返回“ nil”值。

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

ruby hash方法

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值