Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

ruby - Is it possible to refresh ENV during runtime

I am using the dotenv Gem to read variables in my .env file. I created a console command that updates the .env file, but also in the same runtime, it reads them. The problem is that it reads the old values, even though I updated the .env file. Is there a way to refresh the ENV during runtime so it gets the latest values?

.env

FOOBAR=hello

ruby.rb

puts ENV['FOOBAR'] # Prints "hello"
EnvFile.update_variable('.env', 'FOOBAR', "How are you?")
Dotenv.load('.env')
puts ENV['FOOBAR'] # Prints "hello" even though looking in .env it has the new value "How are you?"
question from:https://stackoverflow.com/questions/66050054/is-it-possible-to-refresh-env-during-runtime

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Do you mean you are using dotenv? rbenv manages ruby installs and doesn't read .env from what I searched in the source code. With dotenv, you can reload ENV from the .env file by calling Dotenv.load. It's probably going to give you warnings about constants being redefined. What I would do is make a config class to combine the default settings from ENV with the redefining feature/function you want. Config manages everything but reads defaults from .env.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...