Home » Ruby programming

Hash.default_proc=obj Method with Example in Ruby

Ruby Hash.default_proc=obj Method: Here, we are going to learn about the Hash.default_proc=obj Method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on February 20, 2020

Hash.default_proc=obj Method

In this article, we will study about Hash.default_proc=obj 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.

Method description:

This method is a public instance method that is defined in the ruby library especially for Hash class. This method is used to set the default value and that value will be returned when the key is not found while key lookup. By this method, you can set procs as the default value for any hash object. If you don't set a default value then nil will be returned when the key is not found or key is not a part of the hash instance.

Syntax:

    Hash_object.default_proc

Argument(s) required:

This method does not require any argument. This method is used to assign value.

Example 1:

=begin
  Ruby program to demonstrate default_proc method
=end	

hsh = Hash.new()

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

puts "Hash default_proc implementation"

hsh.default_proc = proc do|hash,key|
	hsh[key]=key+key
end

puts "Hash contents are : #{hsh}"

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

puts "The value of #{ky} is #{hsh[ky]}"

Output

Hash default_proc implementation
Hash contents are : {"color"=>"Black", "age"=>20, "school"=>"Angels' Academy Haridwar", "college"=>"Graphic Era University"}
Enter the key you want to find:
 residence
The value of residence is residenceresidence

Explanation:

In the above code, you can observe that we are setting the proc as the default value of hash with the help of the default_proc method. You can see that when the user has entered a key that is not available in the hash, then the value returned from the proc has been printed.

Example 2:

=begin
  Ruby program to demonstrate setting proc with default method.
=end	

hsh = Hash.new()

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

puts "Hash default implementation"

hsh.default = proc do|hash,key|
hash = key+key
end

puts "Hash contents are : #{hsh}"

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

puts "The value of #{ky} is #{hsh[ky]}"

Output

Hash default implementation
Hash contents are : {"color"=>"Black", "age"=>20, "school"=>"Angels' Academy Haridwar", "college"=>"Graphic Era University"}
Enter the key you want to find:
 animal
The value of animal is #<Proc:0x000056037a3140d8@(repl):14>

Explanation:

In the above code, you can observe that we are trying to set a proc as the default value of the hash object. You can also observe that we are not getting the desired result because you can never set a proc as the default value of any hash with the help of this method. For completing the task, you will always need the default_proc method.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.