The clear
method is used to clear all entries of a hash in Ruby. It removes all hash entries and returns the hash.
hash.clear
This method returns the hash itself.
Let’s look at the code below:
# 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
h1
, h2
, and h3
with some entries.clear
method.When we run the code, the hash entries are cleared using the clear
method.