as_hash ruby_Ruby中带有示例的Hash.merge(other_hash){block}方法

as_hash ruby

Hash.merge(other_hash){block}方法 (Hash.merge(other_hash){block} Method)

In this article, we will study about Hash.merge(other_hash){block} Method. The working of the method can’t be assumed because of it’s quite a different name. Let us read its definition and understand its implementation with the help of syntax and program codes.

在本文中,我们将研究Hash.merge(other_hash){block}方法 。 由于该方法的名称完全不同,因此无法进行假设。 让我们阅读其定义并在语法和程序代码的帮助下了解其实现。

Method description:

方法说明:

This method is a Public instance method and belongs to the Hash class which lives inside the library of Ruby language. This method works in a way that it returns a new hash object which contains the keys and values of self hash as well as another hash. If both the hashes are containing the same keys and values then the new hash will not contain duplicate keys and values or you can say that each key and value will be stored only for once. If you are specifying a block then the value of duplicate keys is known to bypass the value into the block. This method is one of the examples of non-destructive methods where the changes created by the methods are temporary or not permanent.

此方法是Public实例方法,属于Hash类,它位于Ruby语言库中。 此方法的工作方式是返回一个新的哈希对象,该对象包含自身哈希的键和值以及另一个哈希。 如果两个哈希都包含相同的键和值,则新的哈希将不包含重复的键和值,或者可以说每个键和值仅存储一次。 如果要指定一个块,则已知重复键的值会将其绕过该块。 此方法是非破坏性方法的示例之一,其中方法所创建的更改是临时的或非永久的。

Syntax:

句法:

    Hash_object.merge(other_hash){|key,oldval,newval|}

Argument(s) required:

所需参数:

This method only takes one parameter and that argument is nothing but another hash instance you want to merge.

此方法仅使用一个参数,该参数不过是您要合并的另一个哈希实例。

Example 1:

范例1:

=begin
  Ruby program to demonstrate 
  Hash.merge(other_hash){block} method
=end	

hsh = {"colors"  => "red","city"=>"Nainital", "Fruit" => "Grapes", "anything"=>"red","sweet"=>"ladoo"}
hsh1 = {"home" => "shivalik nagar", "city"=>"Haridwar","state"=>"Uttrakhand"}

puts "Hash.merge{block} implementation:"

hash3 = hsh.merge(hsh1){|key,oldval,newval| oldval+newval}

puts "The keys present in the new hash are: #{hash3}"

puts "Original hash : #{hsh}"

Output

输出量

Hash.merge{block} implementation:
The keys present in the new hash are: {"colors"=>"red", "city"=>"NainitalHaridwar", "Fruit"=>"Grapes", "anything"=>"red", "sweet"=>"ladoo", "home"=>"shivalik nagar", "state"=>"Uttrakhand"}
Original hash : {"colors"=>"red", "city"=>"Nainital", "Fruit"=>"Grapes", "anything"=>"red", "sweet"=>"ladoo"}

Explanation:

说明:

In the above code, you can observe that you can merge with another hash with the help of the Hash.merge() method. You can observe that when the duplicate value came, the block has been executed and it has done certain manipulation upon the value of keys. This method is not creating changes in the original hash because this method is an example of non-destructive methods where the changes created by the method are not permanent.

在上面的代码中,您可以观察到可以借助Hash.merge()方法与另一个哈希合并。 您可以观察到,当重复值到达时,该块已执行,并且已对键的值进行了某些操作。 此方法不会在原始哈希中创建更改,因为此方法是非破坏性方法的示例,在该示例中,该方法创建的更改不是永久的。

Example 2:

范例2:

=begin
  Ruby program to demonstrate 
  Hash.merge(other_hash) method
=end	

hsh = {"home" => "shivalik nagar", "city"=>"Haridwar","state"=>"Uttrakhand"}
hsh1 = {"home" => "shivalik nagar", "city"=>"Haridwar","state"=>"Uttrakhand"}

puts "Hash.merge implementation:"

hash3 = hsh.merge(hsh1){|key,oldval,newval| oldval+newval}

puts "The keys present in the new hash are: #{hash3}"

puts "Original hash : #{hsh}"

Output

输出量

Hash.merge implementation:
The keys present in the new hash are: {"home"=>"shivalik nagarshivalik nagar", "city"=>"HaridwarHaridwar", "state"=>"UttrakhandUttrakhand"}
Original hash : {"home"=>"shivalik nagar", "city"=>"Haridwar", "state"=>"Uttrakhand"}

Explanation:

说明:

In the above code, you can observe that you can merge with another hash with the help of the Hash.merge() method. You can observe that when the duplicate value came, the block has been executed and it has done certain manipulation upon the value of keys. This method is not creating changes in the original hash because this method is an example of non-destructive methods where the changes created by the method are not permanent.

在上面的代码中,您可以观察到可以借助Hash.merge()方法与另一个哈希合并。 您可以观察到,当重复值到达时,该块已执行,并且已对键的值进行了某些操作。 此方法不会在原始哈希中创建更改,因为此方法是非破坏性方法的示例,在该示例中,该方法创建的更改不是永久的。

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

as_hash ruby

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值