We can use the clear
method to clear all the entries of a hash.
hash.clear
hash
: This is the hash object we want to clear.
This method returns hash
, but with its entries cleared.
# create some hashesh1 = {one: 1, two: 2, three: 3}h2 = {name: "okwudili", stack: "ruby"}h3 = {"foo": 0, "bar": 1}# print all hashesputs h1puts h2puts h3# clear all hashesh1.clearh2.clearh3.clear# Reprint hashesputs h1puts h2puts h3
In the above code snippet:
h1
, h2
, and h3
with some entries.clear
method.When we run the code, we see that the hash entries are cleared using the clear
method.