Home » Ruby programming

Hash.default=obj Method with Example in Ruby

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

Hash.default=obj Method

In this article, we will study about Hash.default=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. Remember, 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 = object

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 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 = "not available"
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 not available

Explanation:

In the above code, you can observe that we have set a default value with the help of the new method. We are accessing that default value with the help of the default method. The default value is "Not available" and this value will be returned whenever the key is not found in the hash object.

Example 2:

=begin
  Ruby program to demonstrate 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:0x000056392e5a11b8@(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.



Comments and Discussions!

Load comments ↻





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