Schrute
Schrute

Reputation: 731

What's the standard way of specifying the environment for a Ruby app without using RAILS_ENV?

When developing a Ruby application, how can I distinguish in code between development, test and production environments without using RAILS_ENV? I've seen some apps that don't even use Rails using that variable, which doesn't make much sense to me.

Of course I can just use a different name, but is there a standard one? Also, would it be bad to set this on code, in some sort of configuration object, instead of using the system's environment variables?

PS: sorry if this is too basic, but it's hard to search for an answer since the results are always Rails related.

Upvotes: 0

Views: 84

Answers (3)

Neil Slater
Neil Slater

Reputation: 27207

You are free to invent your own semantics, and your own ways of determining which configuration is in use. The environment names of test, development, production have become well-known standards, sometimes with the addition of release-management steps such as smoke, uat, staging etc. However, there is no requirement to use environments as a concept in the first place, nor is there a generic approach that could be applied across all Ruby projects - the set of possible applications is too broad.

If you are creating a web application that conforms to the rack API (for hosting in Apache/Passenger or Thin or other server that supports Rack), it is common to use RACK_ENV environment variable to control the choice of named environment (and which part of config to then use) - Sinatra config will use this for example, and Rails will fall back to it.

Upvotes: 1

Dan Grahn
Dan Grahn

Reputation: 9404

The Standard

Rails.env.development?
Rails.env.test?
Rails.env.production?

Don't use RAILS_ENV

RAILS_ENV is being deprecated and will cause warnings and/or errors.

References

Upvotes: 3

John
John

Reputation: 559

Rails.env = 'production'
Rails.env.production? # true

Upvotes: 0

Related Questions