最佳答案
在 Ruby 中,我尝试编写一行代码,如果已经设置了变量,就使用它,否则默认设置为某个值:
myvar = # assign it to ENV['MY_VAR'], otherwise assign it to 'foobar'
I could write this code like this:
if ENV['MY_VAR'].is_set? #whatever the function is to check if has been set
myvar = ENV['MY_VAR']
else
myvar = 'foobar'
end
但这篇文章相当冗长,我试图用尽可能简洁的方式来写。我怎么能这么做?