Open In App

Ruby | Hash clear() function

Last Updated : 07 Jan, 2020
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
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
# 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
# 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 : {}


Similar Reads