Search code examples
rubydotenv

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?"

Solution

  • You can refresh with the following:

    Dotenv.overload('.env')
    

    Thank you domcermak for answering this at: https://github.com/bkeepers/dotenv/issues/426