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

Ruby Example: Write a program to get value from the hash collection based on specified key.
Submitted by Nidhi, on February 14, 2022

Problem Solution:

In this program, we will create a hash collection and get values from the hash collection using specified keys.

Program/Source Code:

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

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

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

print "Student information: \n";
print student["101"],"\n";
print student["102"],"\n";
print student["103"],"\n";

Output:

Student information: 
Amit
Arun
Sumit

Explanation:

In the above program, we created a hash collection to store student information. Then we get the student's name based on specified keys and printed them.

Ruby Hashes Programs »




Comments and Discussions!

Load comments ↻





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