Ruby Hash clear()函数
clear()是一个Hash类的方法,它可以删除其中的所有Hash元素。
语法。Hash.clear()
参数:要清除的哈希值
返回:清除了所有元素的哈希清除了所有元素的哈希
例子 #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"
输出:
clear a : {}
clear b : {}
clear b : {}
例子#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"
输出:
clear a : {}
clear b : {}
clear b : {}