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

StackOverflow https://stackoverflow.com/questions/20329716

  •  07-08-2022
  •  | 
  •  

Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

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

Rails.env = 'production'
Rails.env.production? # true
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top