Exporting an Environment Variable in Ruby

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



Leave a reply



Submit