Home » Ruby programming

Hash.rassoc(obj) Method with Example in Ruby

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

Hash.rassoc(obj) Method

In this article, we will study about Hash.rassoc(obj) Method. The working of the method can’t be assumed because of 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. The Hash.rassoc() 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 the first contained Hash instance where it found the presence of the value in Key-value pair. It will return "nil" if it hadn't found the value in any of the Hashes.

Syntax:

    Hash_instance.rassoc(obj)

Argument(s) required:

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

Example 1:

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

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

puts "Hash.rassoc implementation:"

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

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

Output

Hash.rassoc implementation:
Enter the Value you want to search:-
 blue
Value not found!

Explanation:

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

Example 2:

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

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

puts "Hash rassoc implementation:"

puts hsh.rassoc("green")

Output

Hash rassoc implementation:

Explanation:

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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





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.