简体   繁体   中英

How to set ruby variables in ruby script to shell environment variables

I want to write a ruby script using fog (a wrapper for aws-sdk). Of course I could hard code my aws secret key and ID but would like to be able to have it set dynamically to my shell env variable since I manage multiple accounts.

require 'fog'
require 'json'
require 'logger'

aws_key_id = $aws_key_id
aws_secret_key = $aws_secret_key
queue_url = $sqs_queue_url

Would this work?

In ruby you use ENV to access those values.

require 'fog'
require 'json'
require 'logger'

aws_key_id = ENV['aws_key_id']
aws_secret_key = ENV['aws_secret_key']
queue_url = ENV['sqs_queue_url']

If you want to provide default values, you can use ENV.fetch('key', [default]) : ENV.fetch('sqs_queue_url', 'http://localhost')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM