Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Ruby | Hash clear() function

  • Last Updated : 07 Jan, 2020

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 : {}




My Personal Notes arrow_drop_up
Recommended Articles
Page :