Use terminal command in ruby code?

This question already has an answer here:

  • Calling shell commands from Ruby 19 answers

  • Kernel#exec , that replaces your ruby process with the one you specified, as a corresponding syscall. Therefore, it ends the program even if there's more code to run. Probably not what you want. Works like: exec("this")
  • Backticks. `this` will run this and return its stdout as a string. The same thing with different syntax: %x(this)
  • Kernel#system : mostly same as exec , but doesn't replace your Ruby process and returns a boolean... most of the time: whether it worked successfully ( true ), it returned non-zero ( false ) or failed to run at all ( nil ); runnable as system("this")
  • See these three and links to more
  • 链接地址: http://www.djcxy.com/p/25288.html

    上一篇: 欺骗一个应用程序,让它认为它的stdout是一个终端,而不是一个管道

    下一篇: 在ruby代码中使用终端命令?