Exporting an Environment Variable in Ruby
Simple answer: You can't.
Longer answer: You can't, unless the operating environment provides hooks to do so. Most do not. The best you can usually do is print out the assignments you want done and have the parent execute them.
Is it possible to export environment property from ruby script?
According to http://ruby.about.com/od/rubyfeatures/a/envvar.htm, you can just write:
ENV['SOME_VAR'] = 'some_value'
Accesing environment variables with $ and ENV in ruby
This is expected behavior. $var
is a global variable in Ruby, and not an environment variable. To access environment variable, as you said, you need to use ENV['var']
.
The environment variables for Ruby on Rails I set on my Ubuntu server are not being read
here is what you could do, open(create one if it does not exist) the .bash_profile
file.
Add all the export lines into the file.
export SENDGRID_USERNAME=""
export SENDGRID_PASSWORD=""
export DATABASE_NAME=""
then save the file and run source ~/.bash_profile
. Then open rails console
and either run ENV
or ENV["SENDGRID_USERNAME"]
, you should see the value you have assigned to the corresponding key.
Also as an alternative we have gem which you could consider once you know how .bash_profile
setup works.
Ruby on Rails environment variable for development environment
Use dotenv
is intended to be used in development:
Add your application configuration to your .env
file in the root of your project.
S3_BUCKET=YOURS3BUCKET
SECRET_KEY=YOURSECRETKEYGOESHERE
You may also add export in front of each line so you can source the file in bash.
in .bashrc
export S3_BUCKET=YOURS3BUCKET
export SECRET_KEY=YOURSECRETKEYGOESHERE
Then access in rails app ENV['S3_BUCKET']
Related Topics
Using Www:Mechanize to Download a File to Disk Without Loading It All in Memory First
How to Create a Sha1 Hash in Ruby
Include Jekyll/Liquid Template Data in a Yaml Variable
Flutter Pod Install Problem - Undefined Method 'Each_Child' For #≪Dir:0X00007Fa6F7E2Ec80≫
Has Anyone Tried Installing Ruby & Rubygems from Source on Ubuntu (Preferably Ubuntu 9)
How to Remove Lines of Data in the Middle of a Text File with Ruby
What Are the Benefits of the New Hash Syntax in Ruby 1.9
Is There a Better Way of Checking Nil or Length == 0 of a String in Ruby
Automatic Counter in Ruby For Each
How to Match Accented Characters With a Regex
How to Get Rid of Non-Ascii Characters in Ruby
Rails Each Loop Insert Tag Every 6 Items
Storing Objects in a Session in Rails
How to Avoid Trailing Empty Items Being Removed When Splitting Strings