Skip to content Skip to sidebar Skip to footer

Ruby Hash Equivalent To Python Dict Setdefault

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':

Solution 1:

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.

Solution 2:

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'

Solution 3:

There is no equivalent to this function in Python. You can always use monkey patching to get this functionality:

classHashdefsetdefault(key, value)ifself[key].nil?
      self[key] = value
    elseself[key]
    endendend

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.

Solution 4:

You can simply pass a block to the Hash constructor:

hash = Hash.newdo |hash, key|
  hash[key] = :defaultend

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

Solution 5:

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]}

Post a Comment for "Ruby Hash Equivalent To Python Dict Setdefault"