Radek
Radek

Reputation: 11121

Can I 'refresh' a value of a variable?

I declare steps that need to be done in order to install wordpress in an array in the beginning of my ruby script

$wordpress_cmds = [ "mkdir -p #{$web_root}#{$web_directory}#{$web_url}/public_html",
                    "cp -R #{$wordpress_current}/*  #{$web_root}#{$web_directory}#{$web_url}/public_html",
                    "chown -R www-data:www-data #{$web_root}#{$web_directory}#{$web_url}",
                 ]

Some of the variables are updated later on. Is there any way to get the latest values of all variables that are in the array when accessing the arrya?

Let's say that if my code is like I always get the initial value of the array not the one I want (with updated variables inside)

$web_root = '====='
$wordpress_cmds = ["#{$web_root}"]

puts $wordpress_cmds[0]
$web_root= "new value"
puts $wordpress_cmds[0] 
$web_root.replace("new value")
puts $wordpress_cmds[0]

Upvotes: 0

Views: 529

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230356

No, string interpolation is one-time operation. It modifies the string by substituting values and that's it. However, with a slightly modified code you can do like this:

$web_root = '====='
$wordpress_cmds = [lambda{"mkdir -p #{$web_root}#{$web_directory}#{$web_url}/public_html"}]

puts $wordpress_cmds[0].call
$web_root= "new value"
puts $wordpress_cmds[0].call 
$web_root.replace("new value")
puts $wordpress_cmds[0].call

Output:

=====
new value
new value

Here instead of string you put a function into array. That function will return an actual value of your variable. So, you have to do $wordpress_cmds[0].call instead of $wordpress_cmds[0] to actually invoke it.

Upvotes: 2

Related Questions