Q:

Ruby program to search an item in the hash collection

belongs to collection: Ruby Hashes Programs

0

In this program, we will create a hash collection then we will search an item into the hash collection using value?() 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 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.

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 get hash collection values as an a... >>
<< Ruby program to convert the hash collection into t...