如果key不存在,则创建默认值

时间:2021-05-17 23:02:26

Can anyone show me a ruby way of checking if a key exists in a hash and if it does not then give it a default value. I'm assuming there is a one liner using unless to do this but I'm not sure what to use.

任何人都可以向我展示一种检查哈希中是否存在密钥的红宝石方法,如果没有,则为其提供默认值。我假设有一个衬垫使用除非这样做,但我不知道该使用什么。

5 个解决方案

#1


45  

If you already have a hash, you can do this:

如果您已经有哈希,可以这样做:

h.fetch(key, "default value")

Or you exploit the fact that a non-existing key will return nil:

或者你利用了一个不存在的密钥将返回nil的事实:

h[key] || "default value"

To create hashes with default values it depends on what you want to do exactly.

要使用默认值创建哈希,它取决于您要完全执行的操作。

  • Independent of key and will not be stored:

    独立于密钥,不会被存储:

    h = Hash.new("foo")
    h[1] #=> "foo"
    h #=> {}
    
  • Dependent on the key and will be stored:

    取决于密钥并将被存储:

    h = Hash.new { |h, k| h[k] = k * k } 
    h[2] #=> 4
    h #=> {2=>4}
    

#2


16  

Constructor of Hash class accept a default value, same will be returned if a matching key is not found.

Hash类的构造函数接受默认值,如果找不到匹配的键,则返回相同的值。

h = Hash.new("default")

h.has_key?("jake")
=> false

h["jake"]
=> "default"

h["jake"] = "26"
h.has_key?("jake")
=> true

#3


11  

If you don't need to store that default value into hash you can use Hash#fetch method:

如果您不需要将该默认值存储到哈希中,则可以使用哈希#fetch方法:

hash = {}
hash.fetch(:some_key, 'default-value') # => 'default-value'
p hash
# => {}

If you need in addition to store your default value every time you access non-existent key and you're the one initializing the hash you can do it using Hash#new with a block:

如果除了每次访问不存在的密钥时都需要存储默认值,并且你是初始化哈希的那个,你可以使用Hash #new和块来实现:

hash = Hash.new { |hash, key| hash[key] = 'default-value' }
hash[:a] = 'foo'
p hash[:b]
# => 'default-value'
p hash
# => { :a => 'foo', :b => 'default-value' }

#4


4  

If you are storing a default value that might be nil and you need to calculate it at storage time:

如果要存储可能为nil的默认值,则需要在存储时计算它:

hash = {}
...
hash.fetch(key) {|k| hash[k] = calc_default()}

#5


1  

If you want to assign the default value to the hash if the key does not exist (yet), you can use the ||= operator, ie:

如果要在键尚不存在的情况下将默认值分配给哈希,则可以使用|| =运算符,即:

h = {some_key: 'non_default_value'}
value = h[:some_key] ||= 'default_value'
value2 = h[:non_existing_key] ||= 'default_value'

value will be assigned 'non_default_value' and value2 will be assigned 'default_value'

值将被指定为'non_default_value',value2将被指定为'default_value'

After these assignments h will have the non_existing_key set to 'default_value':

完成这些分配后,h将non_existing_key设置为'default_value':

{:some_key=>"non_default_value", :non_existing_key=>"default_value"}

{:some_key =>“non_default_value”,:non_existing_key =>“default_value”}

#1


45  

If you already have a hash, you can do this:

如果您已经有哈希,可以这样做:

h.fetch(key, "default value")

Or you exploit the fact that a non-existing key will return nil:

或者你利用了一个不存在的密钥将返回nil的事实:

h[key] || "default value"

To create hashes with default values it depends on what you want to do exactly.

要使用默认值创建哈希,它取决于您要完全执行的操作。

  • Independent of key and will not be stored:

    独立于密钥,不会被存储:

    h = Hash.new("foo")
    h[1] #=> "foo"
    h #=> {}
    
  • Dependent on the key and will be stored:

    取决于密钥并将被存储:

    h = Hash.new { |h, k| h[k] = k * k } 
    h[2] #=> 4
    h #=> {2=>4}
    

#2


16  

Constructor of Hash class accept a default value, same will be returned if a matching key is not found.

Hash类的构造函数接受默认值,如果找不到匹配的键,则返回相同的值。

h = Hash.new("default")

h.has_key?("jake")
=> false

h["jake"]
=> "default"

h["jake"] = "26"
h.has_key?("jake")
=> true

#3


11  

If you don't need to store that default value into hash you can use Hash#fetch method:

如果您不需要将该默认值存储到哈希中,则可以使用哈希#fetch方法:

hash = {}
hash.fetch(:some_key, 'default-value') # => 'default-value'
p hash
# => {}

If you need in addition to store your default value every time you access non-existent key and you're the one initializing the hash you can do it using Hash#new with a block:

如果除了每次访问不存在的密钥时都需要存储默认值,并且你是初始化哈希的那个,你可以使用Hash #new和块来实现:

hash = Hash.new { |hash, key| hash[key] = 'default-value' }
hash[:a] = 'foo'
p hash[:b]
# => 'default-value'
p hash
# => { :a => 'foo', :b => 'default-value' }

#4


4  

If you are storing a default value that might be nil and you need to calculate it at storage time:

如果要存储可能为nil的默认值,则需要在存储时计算它:

hash = {}
...
hash.fetch(key) {|k| hash[k] = calc_default()}

#5


1  

If you want to assign the default value to the hash if the key does not exist (yet), you can use the ||= operator, ie:

如果要在键尚不存在的情况下将默认值分配给哈希,则可以使用|| =运算符,即:

h = {some_key: 'non_default_value'}
value = h[:some_key] ||= 'default_value'
value2 = h[:non_existing_key] ||= 'default_value'

value will be assigned 'non_default_value' and value2 will be assigned 'default_value'

值将被指定为'non_default_value',value2将被指定为'default_value'

After these assignments h will have the non_existing_key set to 'default_value':

完成这些分配后,h将non_existing_key设置为'default_value':

{:some_key=>"non_default_value", :non_existing_key=>"default_value"}

{:some_key =>“non_default_value”,:non_existing_key =>“default_value”}