×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby Hash.default(key=nil) Method

By IncludeHelp Last updated : December 01, 2024

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.

Description and Usage

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)

Parameters

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.