Reputation: 3588
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:
>>> d={'key': 'value'}
>>> d.setdefault('key', 'default')
'value' # returns the existing value
>>> d.setdefault('key-doesnt-exist', 'default')
'default' # sets and returns default value
>>> d
{'key-doesnt-exist': 'default', 'key': 'value'}
Is there an equivalent with Ruby hashes? If not, what is the idiomatic approach in Ruby?
Upvotes: 12
Views: 2915
Reputation: 117
If you only want to modify the value returned by setdefault
, you can express this via Hash#merge!
:
Python:
>>> d = {}
>>> d.setdefault("k", []).append("v")
>>> d
{'k': ['v']}
Ruby:
[28] pry(main)> h = {}
=> {}
[29] pry(main)> h.merge!(k: [:v]) { |_key, old, new| old.concat(new) }
=> {:k=>[:v]}
[30] pry(main)> h
=> {:k=>[:v]}
Upvotes: 1
Reputation: 802
Not to beat a dead horse here, but setDefault acts more like fetch does on a hash. It does not act the same way default does on a hash. Once you set default on a hash, any missing key will use that default value. That is not the case with setDefault. It stores the value for only the one missing key and only if it fails to find that key. That whole stores the new key value pair piece is where it differs from fetch.
At the same time, we currently just do what setDefault does like this:
h = {}
h['key'] ||= 'value'
Ruby continued to drive point home:
h.default = "Hey now"
h.fetch('key', 'default') # => 'value'
h.fetch('key-doesnt-exist', 'default') # => 'default'
# h => {'key' => 'value'}
h['not your key'] # => 'Hey now'
Python:
h = {'key':'value'}
h.setdefault('key','default') # => 'value'
h.setdefault('key-doesnt-exist','default') # => 'default'
# h {'key': 'value', 'key-doesnt-exist': 'default'}
h['not your key'] # => KeyError: 'not your key'
Upvotes: 5
Reputation: 80065
A Hash can have a default value or a default Proc (which is called when a key is absent).
h = Hash.new("hi")
puts h[123] #=> hi
# change the default:
h.default = "ho"
In above case the hash stays empty.
h = Hash.new{|h,k| h[k] = []}
h[123] << "a"
p h # =>{123=>["a"]}
Hash.new([])
would not have worked because the same array (same as identical object) would be used for each key.
Upvotes: 9
Reputation: 29503
There is no equivalent to this function in Python. You can always use monkey patching to get this functionality:
class Hash
def setdefault(key, value)
if self[key].nil?
self[key] = value
else
self[key]
end
end
end
h = Hash.new
h = { 'key' => 'value' }
h.setdefault('key', 'default')
# => 'value'
h.setdefault('key-doesnt-exist', 'default')
# => 'default'
But keep in mind that monkey patching is often seen as a taboo, at least in certain code environments.
The golden rule of monkey patching applies: just because you could, doesn’t mean you should.
The more idiomatic way is to define default values through the Hash constructor by passing an additional block or value.
Upvotes: 3
Reputation: 17020
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
Upvotes: 1