How can I substitute the value of a variable inside the backtick operator?
script_dir = File.expand_path File.dirname(__FILE__) p `ruby -e p "$script_dir"` # this does not work
In Ruby, unlike Perl, the dollar sign indicates a global variable, not a plain regular variable to expand in a string. In a string, you need to use the #{} construct:
p `ruby -e "p #{script_dir}"`
An example:
irb(main):011:0> str = '\'howdy\'' => "'howdy'" irb(main):012:0> `ruby -e "p #{str}"` => "\"howdy\"\n"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With