我正在嘗試使用%x()並使用我之前用來確定要運行的文件的路徑的變量。在ruby中添加%x命令中的變量
基本上我想要做這樣的事情
location = "/home/myhome/somefolder"
%x ("ls " + location)
有沒有辦法做到這一點?
我正在嘗試使用%x()並使用我之前用來確定要運行的文件的路徑的變量。在ruby中添加%x命令中的變量
基本上我想要做這樣的事情
location = "/home/myhome/somefolder"
%x ("ls " + location)
有沒有辦法做到這一點?
你的代碼工作
%x ("ls " + location)
或者
%x ("ls #{location}")
您提供優秀作品的例子,但你應該使用Shellwords
逃避任何變量如
require 'shellwords'
location = '~/My folder with spaces in the name'
%x("ls #{Shellwords::escape(location)}")
謝謝!問題解決了 :) – user1787282 2013-02-13 20:41:52
我使用紅寶石2,沒有適合我的工作上面的。所以我結束了使用類似
eval "%x(ls #{location})"
謝謝,我知道我的方式錯誤是不使用字符串中的正確轉義字符。我需要使用//來表示單斜槓。 – user1787282 2013-02-13 20:41:23