ruby hash方法
Hash.rehash方法 (Hash.rehash Method)
In this article, we will study about Hash.rehash 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.rehash方法 。 由于该方法的名称完全不同,因此无法进行假设。 让我们阅读其定义并在语法和程序代码的帮助下了解其实现。
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 constructs a new hash object which is based on the current hash object values for individual keys. If there is a change introduced in the hash object after they are declared then this method will do a re-indexing of the keys in the hash. You will have to face a run time error if you are trying to invoke this function during the process of traversing the hash instance.
此方法是Public实例方法,属于Hash类,它位于Ruby语言库中。 此方法以构造新哈希对象的方式工作,该对象基于单个键的当前哈希对象值。 如果声明了哈希对象后引入了更改,则此方法将对哈希中的键进行重新索引。 如果在遍历哈希实例的过程中尝试调用此函数,则将面临运行时错误。
Syntax:
句法:
Hash_object.rehash
Argument(s) required:
所需参数:
This method does not require any arguments.
此方法不需要任何参数。
Example 1:
范例1:
=begin
Ruby program to demonstrate Hash.rehash method
=end
a = ["Satyam","Amisha"]
b = ["Nikhil","Saksham"]
hsh = {a=>"friends", b=>"friends"}
puts "Hash elements are: #{hsh}"
puts "Hash.rehash implementation"
b[0]="Hrithik"
puts "Hash after rehash: #{hsh.rehash}"
Output
输出量
Hash elements are: {["Satyam", "Amisha"]=>"friends", ["Nikhil", "Saksham"]=>"friends"}
Hash.rehash implementation
Hash after rehash: {["Satyam", "Amisha"]=>"friends", ["Hrithik", "Saksham"]=>"friends"}
Explanation:
说明:
In the above code, you can observe that you can rehash a hash object with the help of the Hash.rehash() method. We have inserted a new element inside the hash and replaced the current one. We got the result after rehashing the hash instance. 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.rehash()方法重新哈希一个哈希对象。 我们在哈希表中插入了一个新元素,并替换了当前元素。 重新哈希哈希实例后,我们得到了结果。 此方法不会在原始哈希中创建更改,因为此方法是非破坏性方法的示例,在该示例中,该方法创建的更改不是永久的。
Example 2:
范例2:
=begin
Ruby program to demonstrate Hash.rehash method
=end
a = ["Satyam","Amisha"]
b = ["Nikhil","Saksham"]
hsh = {a=>"friends", b=>"friends"}
puts "Hash elements are: #{hsh}"
puts "Hash.rehash implementation"
hsh.each do |key,value| hsh.rehash end
puts "Hash after rehash: #{hsh.rehash}"
Output
输出量
Hash elements are: {["Satyam", "Amisha"]=>"friends", ["Nikhil", "Saksham"]=>"friends"}
Hash.rehash implementation
rehash during iteration
(repl):14:in `rehash'
(repl):14:in `block in <main>'
(repl):14:in `each'
(repl):14:in `<main>'
Explanation:
说明:
In the above code, you can observe that when we are trying to rehash the hash instance during iteration then the method is throwing an exception named RuntimeError. This simply shows that you can’t rehash a hash during the process of iteration.
在上面的代码中,您可以观察到,当我们尝试在迭代过程中重新哈希该哈希实例时,该方法将抛出一个名为RuntimeError的异常。 这仅表明您无法在迭代过程中重新哈希。
翻译自: https://www.includehelp.com/ruby/hash-rehash-method-with-example.aspx
ruby hash方法