Ruby program to delete the item from the hash collection based on a specific key

Ruby Example: Write a program to delete the item from the hash collection based on a specific key.
Submitted by Nidhi, on February 16, 2022

Problem Solution:

In this program, we will create a hash collection and print the created collection. Then we will delete a specific item based on the specific key from the hash collection using the delete() method.

Program/Source Code:

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

# Ruby program to delete item from hash collection 
# based on specific key

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

print "Student information: \n",student;

student.delete("102");

print "\nStudent information after delete() method: \n",student;

Output:

Student information: 
{"101"=>"Amit", "102"=>"Arun", "103"=>"Sumit"}
Student information after delete() method: 
{"101"=>"Amit", "103"=>"Sumit"}

Explanation:

In the above program, we created a hash collection to store student information. Then we deleted the specific item from the hash collection based on a specific key. After that, we printed the updated collection.

Ruby Hashes Programs »




Comments and Discussions!

Load comments ↻





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