Home » Ruby programming

Hash.default(key=nil) Method with Example in Ruby

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

Hash.default(key=nil) Method

In this article, we will study about Hash.default(key=nil) 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 works in a way that it returns the default value but now you will be thinking when this method will return the default value. This method will return the default value when the user is trying to access the value of key or simply key which is not available in the hash or you can say that which is not a part of the hash object. The method will print the default value instead of returning 'nil'.

Syntax:

    Hash_object.default
    or
    Hash_object.default(object)

Argument(s) required:

Passing value in this method is optional. If you are passing a value then that value can be an object of any class.

Example 1:

=begin
  Ruby program to demonstrate default method
=end	

hsh = Hash.new("Not available")

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

puts "Hash default implementation"

puts "Hash contents are : #{hsh}"
puts "default value : #{hsh.default}"

Output

Hash default implementation
Hash contents are : {"color"=>"Black", "age"=>20, "school"=>"Angels' Academy Haridwar", "college"=>"Graphic Era University"}
default value : 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 { |hash, key| hsh[key] = "Hello : #{key}" }

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

puts "Hash default implementation"

puts "Hash contents are : #{hsh}"
puts "default value : #{hsh.default(2)}"

Output

Hash default implementation
Hash contents are : {"color"=>"Black", "age"=>20, "school"=>"Angels' Academy Haridwar", "college"=>"Graphic Era University"}
default value : Hello : 2

Explanation:

In the above code, you can observe that we have set a default value with the help of the new method by passing a block into it. We are accessing that default value with the help of the default method. The default value is "Hello: key" and this value will be returned whenever the key is not found in the hash object. We are passing the key along with the method and that key will be returned along with the value when the key is missing.



Comments and Discussions!

Load comments ↻





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