Q:

Ruby program to get key based on value from the hash collection

belongs to collection: Ruby Hashes Programs

0

In this program, we will create a hash collection. Then we will get the key based on the specified value using the key() method.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to get key based on value from the hash collection is given below. The given program is compiled and executed on Windows 10 Operating System successfully.

# Ruby program to get key based on value
# from hash collection

student = {"101" => "Amit", "102" => "Arun", "103" => "Sumit"};

key = student.key("Amit");

print "Key: ",key;

Output:

Key: 101

Explanation:

In the above program, we created a hash collection to store student information. Then we get the key based on the specified value using the key() method and printed the returned key. It returns nil if no matching value is found.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ruby Hashes Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Ruby program to print the inverted hash collection... >>
<< Ruby program to check a value is exist in the hash...