Customizing variables based on environment

Hi all,

I want to be able to customize a variable based on the environment you
are running in. Is this possible? What is the scope of a variable that
you declare in environment.rb or
environments/{production|development}.rb? I was hoping I can do
something like:

1- Declare a hash in environment.rb and specify most of the properties
i’m interested in, eg:

integration_parameters = { :url => “xyz.com”, :user => “john”,
:password => “secret” }

2- Change whatever is needed, eg in environments/production.rb:

integration_parameters[:password] = “supersecret”

Any ideas on how to do this? Is there a more idiomatic way to do this in
rails?

Thanks,
Abdullah

Abdullah J. wrote:

Hi all,

I want to be able to customize a variable based on the environment you
are running in. Is this possible? What is the scope of a variable that
you declare in environment.rb or
environments/{production|development}.rb? I was hoping I can do
something like:

1- Declare a hash in environment.rb and specify most of the properties
i’m interested in, eg:

integration_parameters = { :url => “xyz.com”, :user => “john”,
:password => “secret” }

2- Change whatever is needed, eg in environments/production.rb:

integration_parameters[:password] = “supersecret”

Any ideas on how to do this? Is there a more idiomatic way to do this in
rails?

Thanks,
Abdullah

In any ruby code, throughout your entire app, you can simply do:

case ENV[‘RAILS_ENV’]
when ‘development’
@foo = ‘d’
when ‘production’
@foo = ‘p’
when ‘test’
@foo = ‘t’
else
raise “Uknown environment #{ENV[‘RAILS_ENV’]}!”
end

I personally use the rails_environments plugin:
http://blog.codahale.com/2006/04/09/rails-environments-a-plugin-for-well-rails/

It provides some super easy helpers like

if Rails.development?
puts ‘We’re in development mode!’
end

Rails.test?
Rails.production?

Makes things a bit easier than the ugly ENV hash.