Open In App

Ruby | Hash clear() function

Last Updated : 07 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

clear() is an Hash class method which removes all the Hash elements from it.

Syntax: Hash.clear()

Parameter: Hashs to clear off

Return: Hash with all elements cleared

Example #1:




# Ruby code for clear() method
  
# declaring Hash value
a = {a:100, b:200}
  
# declaring Hash value
b = {a:100, c:300, b:200}
  
# declaring Hash value
c = {a:100}
  
puts "clear a : #{a.clear()}\n\n"
  
puts "clear b : #{b.clear()}\n\n"
  
puts "clear b : #{c.clear()}\n\n"


Output :

clear a : {}

clear b : {}

clear b : {}

Example #2:




# Ruby code for clear() method
  
# declaring Hash value
a = { "a" => 100, "b" => 200 }
  
# declaring Hash value
b = {"a" => 100}
  
# declaring Hash value
c = {"a" => 100, "c" => 300, "b" => 200}
  
puts "clear a : #{a.clear()}\n\n"
  
puts "clear b : #{b.clear()}\n\n"
  
puts "clear b : #{c.clear()}\n\n"


Output :

clear a : {}

clear b : {}

clear b : {}



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads