在ruby中的%x命令中添加变量

在ruby中的%x命令中添加变量,ruby,Ruby,我正在尝试使用%x()并使用一个我之前使用过的变量来确定要运行的文件的路径 基本上我想做这样的事情 location = "/home/myhome/somefolder" %x ("ls " + location) 有办法做到这一点吗 您的代码工作正常 %x ("ls " + location) 或 您提供的示例运行良好,但您可能应该使用Shellwords来转义任何变量,例如 require 'shellwords' location = '~/My folder with space

我正在尝试使用%x()并使用一个我之前使用过的变量来确定要运行的文件的路径

基本上我想做这样的事情

location = "/home/myhome/somefolder"
%x ("ls " + location)
有办法做到这一点吗

您的代码工作正常

%x ("ls " + location)


您提供的示例运行良好,但您可能应该使用
Shellwords
来转义任何变量,例如

require 'shellwords'

location = '~/My folder with spaces in the name'

%x("ls #{Shellwords::escape(location)}")

我使用的是ruby 2,上面所有的都不适合我。所以我用了类似的东西


eval“%x(ls#{location})”

谢谢,我意识到我的方法中的错误是没有在字符串中使用正确的转义字符。我需要使用//来表示一个斜杠。
require 'shellwords'

location = '~/My folder with spaces in the name'

%x("ls #{Shellwords::escape(location)}")