Reputation: 2485
If I had the following ruby hash:
environments = {
'testing' => '11.22.33.44',
'production' => '55.66.77.88'
}
How would I access parts of the above hash? An example below as to what I am trying to achieve.
current_environment = 'testing'
"rsync -ar root@#{environments[#{testing}]}:/htdocs/"
Upvotes: 13
Views: 12843
Reputation: 34031
It looks like you want to exec
that last line, as it's obviously a shell command rather than Ruby code. You don't need to interpolate twice; once will do:
exec("rsync -ar root@#{environments['testing']}:/htdocs/")
Or, using the variable:
exec("rsync -ar root@#{environments[current_environment]}:/htdocs/")
Note that the more Ruby way is to use Symbols rather than Strings as the keys:
environments = {
:testing => '11.22.33.44',
:production => '55.66.77.88'
}
current_environment = :testing
exec("rsync -ar root@#{environments[current_environment]}:/htdocs/")
Upvotes: 8
Reputation: 59273
You would use brackets:
environments = {
'testing' => '11.22.33.44',
'production' => '55.66.77.88'
}
myString = 'testing'
environments[myString] # => '11.22.33.44'
Upvotes: 5