Rails: difference between ENV.fetch() and ENV[]
The ENV
hash-like object is plain Ruby, not part of Rails. From the fine ENV#[]
manual:
Retrieves the value for environment variable
name
as a String. Returnsnil
if the named variable does not exist.
and the fine ENV#fetch
manual:
Retrieves the environment variable
name
.If the given name does not exist and neither
default
nor a block a provided an IndexError is raised. If a block is given it is called with the missing name to provide a value. If a default value is given it will be returned when no block is given.
So just like Hash#[]
and Hash#fetch
, the only difference is that fetch
allows you to specify the behavior if a key is not found (use a default value passed to fetch
, default block passed to fetch
, or raise an exception) whereas []
just silently gives you nil
if the key isn't found.
In the specific case of:
ENV.fetch("MY_VAR")
ENV['MY_VAR']
the difference is that ENV['MY_VAR']
will give you nil
if there is no MY_VAR
environment variable but ENV.fetch('MY_VAR')
will raise an exception.
What is difference between ENV.fetch with block or with second param?
The difference is that the missing variable name is yielded to the block.
In your example, the result is the same because you do not use the yielded string, but try this to see the difference:
ENV.fetch("RAILS_MAX_THREADS", 5)
#=> 5
ENV.fetch("RAILS_MAX_THREADS") { |missing_name| "Could not find env var named " + missing_name }
#=> "Could not find env var named RAILS_MAX_THREADS"
Ruby: Most concise way to use an ENV variable if it exists, otherwise use default value
myvar = ENV['MY_VAR'] || 'foobar'
N.B. This is slightly incorrect (if the hash can contain the value nil
) but since ENV
contains just strings it is probably good enough.
What is the and why is there a difference between request.env['CONTENT_TYPE'] and request.content_type?
request.env
contains Rack's "thoughts" on what the content type is. Generally, this is the content type of the request that you have made.
request.content_type
on the other hand is Rails's interpretation of what it thinks the content type is, based off the format of the request. These are defined in a file called mime_types.rb
in Rails (I can't recall which part, but with that you should be able to locate it), and additional ones can be specified in config/initializers/mime_types.rb
.
Where to define env variables and how to fetch them in Rails
Use the figaro gem
Add figaro gem to your Gemfile and bundle install
Run figaro install
. This will create a file named application.yml in config directory and it will add this file to the .gitignore file so that git does not track this file and you don not end up sending the file to to the remote repo.
You put your keys in config/application.yml in this form
user_name: "very_long_user_name!!@$#!@$#$"
password: "long_password_7381a978f7dd7f9a1117"
Wherever you want to use the user_name or password
ENV["user_name"]
ENV["password"]
To set your keys on heroku run figaro heroku:set -e production
rails 3, how use an ENV config vars in a Settings.yml file?
use following:-
default:
cv1: Foo
cv2: <%= ENV['FOOVAR'] %>
Related Topics
Using Activerecord Interface for Models Backed by External API in Ruby on Rails
Sleep Until Condition Is True in Ruby
Why Does Date Exist in Ruby Before It Is Required
Memory Usage Increase with Ruby 2.1 Versus Ruby 2.0 or 1.9
Solving the Travelling Salesman Problem in Ruby (50+ Locations)
Cheat Sheet for All Design Patterns Implemented in Ruby
Ruby: How to Determine If File Being Read Is Binary or Text
Activerecord Association Select Counts for Included Records
Custom Rails Configuration Section
Collecting Hashes into Openstruct Creates "Table" Entry
Dynamically Generate Scopes in Rails Models
Require Lib in Rspec with Ruby 1.9.2 Brings "No Such File to Load"