How to invoke a Ruby script from Python?

To invoke a Ruby script in Python, you can utilize the subprocess module. Here is a simple example:

import subprocess

# 调用Ruby脚本
result = subprocess.run(['ruby', 'script.rb'], capture_output=True, text=True)

# 打印Ruby脚本的输出
print(result.stdout)

In this example, the subprocess.run method is used to run a subprocess that executes a specific command. The command is passed as a list, with the first element being the path to the Ruby interpreter and the second element being the path to the Ruby script to be executed. The parameters capture_output=True is used to capture the output of the subprocess, and text=True is used to decode the output as text.

You can adjust the commands and parameters according to your needs to suit specific Ruby scripts.

bannerAds