Home » Ruby programming

Hash.assoc() Method with Example in Ruby

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

Hash.assoc() Method

In this article, we will study about Hash.assoc() 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. 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 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)

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.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

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

RUN 2:
Hash.assoc implementation:
Enter the Key you want to search:
 veges
Key not found!

Explanation:

In the above code, you can find that the Hash instance on which we have invoked assoc() method is not any normal Hash instance. It is 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 key inputted by the user.

Example 2:

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

hsh = {"color"=> "green","vege"=> "papaya"}

puts "Hash assoc implementation:"

puts hsh.assoc("color")

Output

Hash assoc implementation:
color
green

Explanation:

In the above you can verify that assoc() method also works upon normal Hash instances. It will return the key-value pairs if the key is a part of the Hash instance.



Comments and Discussions!

Load comments ↻





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