ruby hash方法_Ruby中带有示例的Hash.replace()方法

ruby hash方法

Hash.replace()方法 (Hash.replace() Method)

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

Method description:

方法说明:

This method is a public instance method that is defined in the ruby library especially for 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 replace the content of the current hash with the contents of the hash on which the method has been invoked.

此方法是在ruby库中定义的公共实例方法,特别是针对Hash类。 由于此方法是破坏性方法的示例,因此此方法创建的更改是永久的或非临时的。 此方法的工作方式是将当前哈希的内容替换为已在其上调用该方法的哈希的内容。

Syntax:

句法:

    Hash_object.replace(other_hash)

Argument(s) required:

所需参数:

This method only takes one parameter and that argument is nothing but the other hash whose content you want to store in the main hash.

此方法仅使用一个参数,而该参数则是您要存储在主哈希中的内容的另一个哈希。

Example 1:

范例1:

=begin
  Ruby program to demonstrate replace method
=end  

hsh = Hash.new()

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

puts "Hash contents before replace method are : #{hsh}"

hsh2 = {"name"=>"Hrithik","class"=>"bca","age"=>20}

puts "Hash replace implementation"

puts "#{hsh.replace(hsh2)}"

puts "Hash contents after replace method are : #{hsh}"

Output

输出量

Hash contents before replace method are : {"color"=>"Black", "age"=>20, "school"=>"Angels' Academy Haridwar", "college"=>"Graphic Era University"}
Hash replace implementation
{"name"=>"Hrithik", "class"=>"bca", "age"=>20}
Hash contents after replace method are : {"name"=>"Hrithik", "class"=>"bca", "age"=>20}

Explanation:

说明:

In the above code, you can observe that we can replace the contents of one hash with the contents of another hash object with the help of the Hash.replace() method. You may observe that you can pass a hash object with the method at the time of its invocation. The method is creating permanent change on the hash object.

在上面的代码中,您可以观察到,借助于Hash.replace()方法 ,我们可以将一个哈希的内容替换为另一个哈希对象的内容。 您可能会注意到,可以在调用该方法时将散列对象与该方法一起传递。 该方法在哈希对象上创建永久更改。

Example 2:

范例2:

=begin
  Ruby program to demonstrate replace method
=end  

hsh = Hash.new()

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

puts "Hash replace implementation"

puts "Hash contents before replace method are : #{hsh}"

puts "#{hsh.replace({"name"=>"Hrithik","class"=>"bca","age"=>20})}"

puts "Hash contents after replace method are : #{hsh}"

Output

输出量

Hash replace implementation
Hash contents before replace method are : {"color"=>"Black", "age"=>20, "school"=>"Angels' Academy Haridwar", "college"=>"Graphic Era University"}
{"name"=>"Hrithik", "class"=>"bca", "age"=>20}
Hash contents after replace method are : {"name"=>"Hrithik", "class"=>"bca", "age"=>20}

Explanation:

说明:

In the above code, you can observe that we can replace the contents of one hash with the contents of another hash object with the help of the Hash.replace() method. You may observe that you can pass multiple keys and values with the method at the time of its invocation. The method is creating permanent change on the hash object.

在上面的代码中,您可以观察到,借助于Hash.replace()方法 ,我们可以将一个哈希的内容替换为另一个哈希对象的内容。 您可能会注意到,在调用该方法时,可以使用该方法传递多个键和值。 该方法在哈希对象上创建永久更改。

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

ruby hash方法

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值