Tips – Set Default Value for A Hash

RubyOnRails,Tips 10 六月 2010 | View Comments


Can hash have a default value, when it doesn’t have a key?

Answer is yes!

h = Hash.new("default")
h["wrong_value"] # => "default"



Tagged in , ,

blog comments powered by Disqus

Ads Plugin created by Jake Ruston's Wordpress Plugins - Powered by and football database.