Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substituting the value of variable inside backtick operator

Tags:

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 
like image 773
Talespin_Kit Avatar asked Jul 16 '13 12:07

Talespin_Kit


1 Answers

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" 
like image 77
Bogatyr Avatar answered Oct 03 '22 22:10

Bogatyr