default= Sets the default value : default « Hash « Ruby






default= Sets the default value


h = { "a" => 100, "b" => 200 }
h.default = "Go fish"
h["a"]  
h["z"]  
# This doesn't do what you might hope...
h.default = proc do |hash, key|
    hash[key] = key + key
end

puts h[2] 
puts h["cat"] 

 








Related examples in the same category

1.default(key=nil ) Returns the default value