In Python it is possible to read a dictionary/hash key while at the same time setting the key to a default value if one does not already exist.
For example:
You can simply pass a block to the Hash constructor:
hash = Hash.new do |hash, key|
hash[key] = :default
end
The block will be invoked when an attempt to access a non-existent key is made. It will be passed the hash object and the key. You can do anything you want with them; set the key to a default value, derive a new value from the key, etc.
If you already have a Hash object, you can use the default_proc= method:
hash = { key: 'value' }
# ...
hash.default_proc = proc do |hash, key|
hash[key] = :default
end