Home » Ruby programming

Hash.fetch() Method with Example in Ruby

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

Hash.fetch() Method

In this article, we will study about Hash.fetch() 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 requires keys whose values are fetched by this method. This method works in the way that it returns the value or values from the hash object for a given key or keys. In case the key is not found in the hash instance then the result is dependent upon the type of method call done. The possible method calls are listed below:

  • Without any other argument: If no other argument is provided and the key is not found then the method will throw a "KeyError" exception.
  • With other arguments (s): The other argument is considered as the default argument. If the key is not found then that default argument will be returned from the method.
  • With block: If the block is provided and the key is not found then that block will be run and the result will be returned in case the key is not found.

Syntax:

    Hash_object.fetch(…,[default]);
    or
    Hash_object_fetch(key(s))
    or
    Hash_object_fetch{|key| block}

Argument(s) required:

There is no restriction upon passing the arguments. You can pass arguments as per your requirement. The last argument will always be considered as the default argument.

Example 1:

=begin
  Ruby program to demonstrate fetch method
=end	

hash1={"color"=>"Black","object"=>"phone","love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}

puts "Hash.fetch implementation"

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

if(hash1.fetch(ky))
	puts "Key found successfully. The value is #{hash1.fetch(ky)}"
else
	puts "No key found"
end

Output

RUN 1:
Hash.fetch implementation
Enter the key you want to search:
 color
Key found successfully. The value is Black

RUN 2:
Hash.fetch implementation
Enter the key you want to search:
 velvet
key not found: "velvet"
(repl):12:in `fetch'
(repl):12:in `<main>'

Explanation:

In the above code, you can simply observe that we have invoked the method with a key which is being asked by the user. In Run 2, you may see that the exception is thrown when the key is not found in the hash.

Example 2:

=begin
  Ruby program to demonstrate fetch method
=end	

hash1={"color"=>"Black","object"=>"phone","love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}

puts "Hash.fetch implementation"

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

puts "The value of #{ky} is #{hash1.fetch(ky,"Not found")}"

Output

Hash.fetch implementation
Enter the key you want to search:
 cloth
The value of cloth is Not found

Explanation:

In the above code, you can observe that we have passed a default argument along with the key inside the method. You can see that the default object is returned when the key is not found in the hash instance.

Example 3:

=begin
  Ruby program to demonstrate fetch method
=end	

hash1={"color"=>"Black","object"=>"phone","love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}

puts "Hash.fetch implementation"

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

puts "The value of #{ky} is #{hash1.fetch(ky){|ky| "Sorry! #{ky} is missing"}}"

Output

Hash.fetch implementation
Enter the key you want to search:
 cloth
The value of cloth is Sorry! cloth is missing

Explanation:

In the above code, you can observe that we are passing a block along with the method. That block has been run and its value has been returned when the key is not found in the hash object.



Comments and Discussions!

Load comments ↻





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