Home » Ruby programming

Hash.rehash Method with Example in Ruby

Ruby Hash.rehash Method: Here, we are going to learn about the Hash.rehash Method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on March 13, 2020

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.

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.

Syntax:

    Hash_object.rehash

Argument(s) required:

This method does not require any arguments.

Example 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.

Example 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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.