Aaron
Aaron

Reputation: 6704

Ruby on Rails environment variables in layman's terms

Can someone explain to me what exactly Ruby on Rails environment variables are, what they do, and how to configure/access them with a concrete example? I see them everywhere in well developed Rails apps but I don't have them figured out.

Some questions:

Here is an example I've found that i don't understand:

located in the Gemfile:

if ENV['RAILS_PANEL_ENABLED'] gem 'meta_request' end

Or, if anyone has any resource to point me to that explains these points well, that would be appreciated also. I haven't found a clear and broad explanation yet.

Emphasis is placed on the why over the how/what/where.

Thank you.

Upvotes: 0

Views: 104

Answers (2)

colinm
colinm

Reputation: 4288

They're not Rails environment variables; they're just plain old environment variables.

When a Ruby interpreter starts up, it loads most available environment variables into the ENV object. For convenience, a number of uninteresting variables (like your bash shell string) are filtered out. You're then left with a hash-like object that can access useful environment variables at will.

This has the benefit of separating configuration from code and also offers an easy way to keep sensitive data (like authentication tokens) out of source control.

Upvotes: 0

Orlando
Orlando

Reputation: 9692

Using environment variables is a good pratice since you don't need to change the code to change behaviour. Is really useful for handling stuff like credentials and configuration (in case of credentials has an additional benefit, you don't put sensible information in your source control repository).

Gems like dotenv-rails makes a lot easier to handle env variables, you should give it a look.

Upvotes: 1

Related Questions