ADVERTISEMENT
ADVERTISEMENT

How to check if a hash key exists in Ruby?

Here, we are going to learn how to check whether a hash key exists or not in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on May 07, 2020

We know that Hashes are the instances which are the combination of keys and their values. A particular key in hash may contain more than one value as well. In this article, we will see how we can check or test the presence of a particular key in hash object? There are multiple ways to tackle this problem.

Let us discuss a few of them,

Method 1: With the help of has_key?() method

This method is a Public instance method and belongs to the Hash class which lives inside the library of Ruby language. Hash.has_key?() method is used to check whether a key(key-value) is a part of the particular Hash instance or not and that Hash instance should be a normal Hash instance. It will search through the whole Hash and gives you the result according to its search. Let us go through the syntax and demonstrating the program codes of this method.

If you are thinking about what it will return then let me tell you, it will return a Boolean value. The returned value will be true if it finds the key inside the Hash and the return value will be false if it does not find the key to be the part of Hash instance.

Syntax:

    Hash_instance.has_key?(obj)

Parameter(s) required:

This method only takes one parameter and that argument is nothing but the key whose presence we want to check.

Program:

=begin
  Ruby program to demonstrate Hash.has_key() method
=end	

hsh = {"colors"  => "red",
     "letters" => "a", "Fruit" => "Grapes"}

puts "Hash.has_key implementation:"
puts "Enter the Key you want to search:-"
ky = gets.chomp

if (hsh.has_key?(ky))
	puts "Key found successfully"
else
	puts "Key not found!"
end

Output

Hash.has_key implementation:
Enter the Key you want to search:-
 colors
Key found successfully

Explanation:

In the above code, you can observe that we are invoking the Hash.has_key?() method on the normal Hash instance. It has returned true when it found the presence of the key in the Hash object which is entered by the user.

Method 2: With the help of assoc() method

The above method will only tell you the presence of key but assoc() method will return the set of values associated with that particular key.

This method is a public instance method and belongs to the Hash class which lives inside the library of Ruby language. This method is used to check whether an object(key-value) is a part of the particular Hash instance or not and that Hash instance can or cannot be the normal Hash instance. If it is not normal, it means that Hash instance is the Hash of multiple Array instances along with their keys or you can say that it the collection of multiple keys and values which are itself an object of Array class. Let us go through the syntax and demonstrating the program codes of this method.

If you are thinking about what it will return then let me tell you, it will return the first contained Hash instance where it found the presence of the Key-value pair. It will return 'nil' if it hadn't found the object in any of the Hashes.

Syntax:

    Hash_instance.assoc(obj)

Parameter(s) required:

This method only takes one parameter and that argument is nothing but an object whose presence we want to check.

Program:

=begin
  Ruby program to demonstrate Hash.assoc() method
=end
hsh = {"colors"  => ["red", "blue", "green"],
     "letters" => ["a", "b", "c" ], "Fruit" => ["Banana","Kiwi","Grapes"]}

puts "Hash.assoc implementation:"
puts "Enter the Key you want to search:-"
ky = gets.chomp

if (hsh.assoc(ky))
	puts "Key found successfully"
	puts "Values are: #{hsh.assoc(ky)}"
else
	puts "Key not found!"
end

Output

Hash.assoc implementation:
Enter the Key you want to search:-
 colors
Key found successfully
Values are: ["colors", ["red", "blue", "green"]]

Method 3 : With the help of member?() method

This method is a public instance method and belongs to the Hash class which lives inside the library of Ruby language. Hash.member?() method is used to check whether a key(key-value) is a part of the particular Hash instance or not and that Hash instance should be the normal Hash instance. It will search through the whole Hash and gives you the result according to its search. Let us go through the syntax and demonstrating the program codes of this method.

If you are thinking about what it will return then let me tell you, it will return a Boolean value. The returned value will be true if it finds the key inside the Hash and the return value will be false if it does not find the key to be the part of Hash instance.

Syntax:

    Hash_instance.member?(obj)

Parameter(s) required:

This method only takes one parameter and that argument is nothing but the key whose presence we want to check.

Program:

=begin
  Ruby program to demonstrate Hash.member method
=end	

hsh = {"colors"  => "red",
     "letters" => "a", "Fruit" => "Grapes"}

puts "Hash.member? implementation:"
puts "Enter the Key you want to search:-"
ky = gets.chomp

if (hsh.member?(ky))
	puts "Key found successfully"
else
	puts "Key not found!"
end

Output

Hash.member? implementation:
Enter the Key you want to search:-
 colors
Key found successfully

Explanation:

In the above code, you can observe that we are invoking the Hash.member?() method on the normal Hash instance. It has returned true when it found the presence of the key in the Hash object which is entered by the user.

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.