Ruby hash equivalent to Python dict setdefault

后端 未结 5 1908
悲&欢浪女
悲&欢浪女 2021-01-17 12:30

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:

相关标签:
5条回答
  • 2021-01-17 12:58

    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.

    0 讨论(0)
  • 2021-01-17 13:03

    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.

    0 讨论(0)
  • 2021-01-17 13:03

    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
    
    0 讨论(0)
  • 2021-01-17 13:14

    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'
    
    0 讨论(0)
  • 2021-01-17 13:14

    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]}
    
    0 讨论(0)
提交回复
热议问题