Q:

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

belongs to collection: Ruby Hashes Programs

0

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.

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 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.

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 check a hash collection is empty o... >>
<< Ruby program to remove all items from the hash col...