Ruby: Most concise way to use an ENV variable if it exists, otherwise use default value

后端 未结 9 1106
小鲜肉
小鲜肉 2020-12-23 08:59

In Ruby, I am trying to write a line that uses a variable if it has been set, otherwise default to some value:

myvar = # assign it to ENV[\'MY_VAR\'], otherw         


        
9条回答
  •  臣服心动
    2020-12-23 09:35

    Another possible alternative, which will work even if ENV['MY_VAR'] turnsout to be a false value

    myvar  = ENV['MY_VAR'].presence || 'foobar'
    

提交回复
热议问题