Last Updated: February 25, 2016
·
866
· cloudsplitter

Environment Variables in your Rails App

Something simple that someone asked me yesterday. It's a simple concept but might not be fully obvious to everyone.

You can start your Rails app with preset environment variables form the command line. This can be useful for setting up your own custom debug mode for instance.

MY_SPECIAL_ENV=freakout rails s

Or maybe you want many of theme because you miss Java.

RUBY_LANG=nojava RUBY_VERSION=876.2.1 DDEBUG=serious rails s

To use these variables in your application, just access them using the ENV array.

puts ENV['MY_SPECIAL_ENV']

Also, remember that you can change the Rails env using this method.

RAILS_ENV=test rails s