Call a ruby script from within a rails app controller

I'm very new to rails and I have a script that I run from the console like this

   $ ruby axml2xml.rb ExamPaper.apk

Now, how do I call this script from within my controller method and pass the same parameter as ExamPaper.apk ?

I tried require 'axml2xml.rb' but got some error pointing to this line of code Zip::ZipFile.foreach(ARGV[0]) do |f| . So basically, how do I make something like axml2xml.rb 'ExamPaper.apk' in my controller?


You have at least 3 options:

  • exec(command)
  • %x{ command }
  • system(command)
  • They have different behaviors, so make sure to read this quicktip and/or the answer of this question to learn more about these commands.

    In your case, the backticks or %x command is probably the best option.

    value = `ruby axml2xml.rb ExamPaper.apk`
    

    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.


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

    exec 'ruby axml2xml.rb ExamPaper.apk'
    
    链接地址: http://www.djcxy.com/p/25304.html

    上一篇: 从Ruby执行grep

    下一篇: 从Rails应用程序控制器中调用ruby脚本