← Back to Gists

hash default value

📝 Ruby
diana49945
diana49945 · Level 8 ·

In Ruby, setting a hash's default value lets you specify a fallback for missing keys instead of returning nil.

Ruby
h = Hash.new("default")
h[:a] = 1
puts h[:a] # Output: 1
puts h[:b] # Output: default

Comments

No comments yet. Start the discussion.