ADVERTISEMENT
ADVERTISEMENT

Ruby program to search an item in the hash collection

Ruby Example: Write a program to search an item in the hash collection.
Submitted by Nidhi, on February 18, 2022

Problem Solution:

In this program, we will create a hash collection then we will search an item into the hash collection using value?() method.

Program/Source Code:

The source code to search an item in the hash collection is given below. The given program is compiled and executed on Windows 10 Operating System successfully.

# Ruby program to search an item 
# in hash collection

hash = {"101" => "Arun", "102" => "Sumit", "103" => "Mukesh"};

print "Enter item: ";
item = gets.chomp;  

if hash.value?(item)==true 
    printf "The item '%s' found in hash collection",item;
else
    printf "Item %s not found",item;
end

Output:

Enter item: Sumit
The item 'Sumit' found in hash collection

Explanation:

In the above program, we created a hash collection to store student information. Then we used value?() method to search an item into the hash collection. The value?() method returns true if ITEM is found in the hash collection, otherwise, it returns a false and printed appropriate message.

Ruby Hashes Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

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.