Search code examples
rubysinatrabundlerirb

Automatically load Dotenv on my ruby console


I'd like to automatically run Dotenv.load('.env.development') whenever I launch up a ruby console, it could either be from bundle console or alternatively irb. I'm using Sinatra, not Rails, and I'm not sure how to run some commands on console start.. I'd prefer to do this without a bash script, instead using the internal capabilities of the tools.. If there's a place to put ruby code on the start of a ruby console that would solve my issue and also allow for future console customization.


Solution

  • You can create a .irbrc file in our project's directory which is loaded automatically when an IRB session is started. And add something like this to that file:

    begin
      require 'dotenv'
      Dotenv.load('.env.development')
    rescue => e
      puts "loading Dotenv failed. because: #{e.message}"
    end
    

    Read more about the .irbrc file in the Ruby-Docs.