I'm making a simple RubyGem (no Rails) that uses the Mail gem to send out emails from the CLI. In order to configure the mailer I have to supply a password, which I want to store secretly in an ENV variable or something so that it's not public.
I tried to use Dotenv but found that after installing my newly built gem, it only works when I am running it from the project directory. If I try to run the gem from my home directory, the ENV variable is no longer accessible.
In the gemspec spec.files I made sure to include the .env file.
I don't think I'm understanding the actual process for loading variables into a RubyGem.
The fact that the correct behaviour depends on the current working directory at time of invocation suggests that this is a relative path issue. Since it works at least some of the time, your
gemspecsetup must be correct.I haven't used
Dotenvmyself, but looking through the source suggests that giving an explicit path to yourenvfile throughDotenv.loadis the way forward.Try using
Dotenv.load(File.expand_path("../.env", __FILE__))frombin/myapp, or adapt to your directory structure (assuming theenvfile is in Gem root).A similar approach is discussed in this tutorial.