Home » Ruby programming

Hash.has_value?() Method with Example in Ruby

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

Hash.has_value?() Method

In this article, we will study about Hash.has_value?() Method. The working of the method can't be assumed because it's quite a different name. Let us read its definition and understand its implementation with the help of syntax and program codes.

Method description:

This method is a Public instance method and belongs to the Hash class which lives inside the library of Ruby language. Hash.has_value? method is used to check whether a value(key-value) is a part of the particular Hash instance or not and that Hash instance should be the normal Hash instance. An abnormal means that Hash instance is the Hash of multiple Array instances along with 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 Boolean values that are namely "true" and "false". It will return "true" if the value is present in the Hash and it will return "false" if it is unable to find the value inside the hash.

Syntax:

    Hash_instance.has_value?(obj)

Argument(s) required:

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

Example 1:

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

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

puts "Hash.has_value implementation:"

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

if (hsh.has_value?(ky))
	puts "value found successfully"
else
	puts "value not found!"
end

Output

RUN 1:
Hash.has_value implementation:
Enter the value you want to search:
 red
value found successfully

RUN 2:
Hash.has_value implementation:
Enter the value you want to search:
 Yellow
value not found!

Explanation:

In the above code, you can find that the Hash instance on which we have invoked the Hash.has_value?() method is normal. It is not the collection of multiple Array instances along with their specific keys. It is returning true where it has found the value inputted by the user.

Example 2:

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

hsh = {"color"=> ["green","blue","yellow"],"vege"=> ["papaya","brinjal"]}

puts "Hash has_value implementation:"

puts hsh.has_value?("green")

Output

Hash has_value implementation:
false

Explanation:

In the above, you can verify that has_value?() method does not work upon abnormal Hash instances. It will return nil even if the value is present in the Hash.



Comments and Discussions!

Load comments ↻





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