We can use the delete(key)
method to delete an entry of a hash. It returns the associated value of the entry deleted.
hash.delete(key)
hash
: The hash we want to delete an entry from.
This method returns the value of the entry deleted.
Let’s look at the code below:
# create hashesh1 = {one: 1, two: 2, three: 3}h2 = {name: "okwudili", stack: "ruby"}h3 = {"foo": 0, "bar": 1}# delete some keys# and print return valuesputs h1.delete(:three)puts h2.delete(:stack)puts h3.delete(:bar)# now print hashesputs h1puts h2puts h3
h1
, h2
, and h3
.delete(key)
method on the hashes and print their returned values.