Home » Ruby programming

Hash.fetch_values() Method with Example in Ruby

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

Hash.fetch_values() Method

In this article, we will study about Hash.fetch_values() 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 array instance containing value or values from the hash object for a given key or keys. In case the key is not found in the hash object, an exception is thrown by the method and it is termed as "keyless" exception. For doing some kind of manipulation with the key values, you can even pass a block with the method.

Syntax:

    Hash_object.fetch_values(key1, key2,…, keyn)
    or
    Hash_object_fetch_values(key1, key2,…, keyn){|key| block}

Argument(s) required:

There is no restriction upon passing the arguments. You can pass arguments as per your requirement.

Example 1:

=begin
  Ruby program to demonstrate fetch_values method
=end	

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

puts "Hash.fetch_values implementation"

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

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

if(hash1.fetch_values(ky1,ky2))
	puts "Key found successfully. The values ar #{hash1.fetch_values(ky1,ky2)}"
else
	puts "One of the keys are missing"
end

Output

RUN 1:
Hash.fetch_values implementation
Enter the first key you want to search:
 color
Enter the second key you want to search:
 object
Key found successfully. The values ar ["Black", "phone"]

RUN 2:
Hash.fetch_values implementation
Enter the first key you want to search:
 cloth
Enter the second key you want to search:
 color
key not found: "cloth"
(repl):15:in `fetch_values'
(repl):15:in `<main>'

Explanation:

In the above code, you may observe that this method returns an array of the values. These values are nothing but the values of the key passed along with the method. In Run 2, you can observe that even if the single key is missing the method will throw a "Keyless" exception

Example 2:

=begin
  Ruby program to demonstrate fetch_values method
=end	

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

puts "Hash.fetch_values implementation"

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

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

if(hash1.fetch_values(ky1,ky2){|ky| ky.upcase})
	puts "Key found successfully. The values are #{hash1.fetch_values(ky1,ky2){|ky| ky.upcase}}"
else
	puts "One of the keys are missing"
end

Output

Hash.fetch_values implementation
Enter the first key you want to search:
 color
Enter the second key you want to search:
 car
Key found successfully. The values are ["Black", "CAR"]

Explanation:

In the above code, you can observe that when we are passing a block then the key which is not found is converted into uppercase. This protects us from getting the exception.




Comments and Discussions!

Load comments ↻






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