2

我对 Rails 很陌生,我有一个像这样从控制台运行的脚本

   $ ruby axml2xml.rb ExamPaper.apk

现在,如何从我的控制器方法中调用此脚本并传递与ExamPaper.apk

我试过require 'axml2xml.rb'了,但有一些指向这行代码的错误Zip::ZipFile.foreach(ARGV[0]) do |f|。所以基本上,我如何axml2xml.rb 'ExamPaper.apk'在我的控制器中制作类似的东西?

4

4 回答 4

5

您至少有 3 个选项:

  • exec(command)
  • %x{ command }
  • system(command)

它们有不同的行为,因此请务必阅读此快速提示和/或此问题的答案,以了解有关这些命令的更多信息。

在您的情况下,反引号或%x命令可能是最好的选择。

value = `ruby axml2xml.rb ExamPaper.apk`
于 2012-07-04T08:58:16.507 回答
1

You can try using system or popen, but only for short tasks, for more information about that, please see here.

If your task is more time consuming you definitely should have a look at something like delayed_job and use a background job or some sort of queue to run your job. This way your server doesn't get blocked and your users do not have to wait til your job completes.

于 2012-07-04T08:59:15.107 回答
0

如果要将其作为 shell 命令执行,请使用:

exec 'ruby axml2xml.rb ExamPaper.apk'
于 2012-07-04T08:54:28.103 回答
0

在 ruby​​ 中,有几种方法可以运行 shell 命令。

system("ls")
%x("ls")
`ls`   #nice they are back ticks
exec "ls"

But I'm not sure about the permissions necessary for running commands like that via rails.

于 2012-07-04T08:58:36.517 回答