How to call shell commands from Ruby

前端 未结 20 2343
旧时难觅i
旧时难觅i 2020-11-22 01:10

How do I call shell commands from inside of a Ruby program? How do I then get output from these commands back into Ruby?

20条回答
  •  离开以前
    2020-11-22 01:53

    You can also use the backtick operators (`), similar to Perl:

    directoryListing = `ls /`
    puts directoryListing # prints the contents of the root directory
    

    Handy if you need something simple.

    Which method you want to use depends on exactly what you're trying to accomplish; check the docs for more details about the different methods.

提交回复
热议问题