2009-05-07 4 views

答えて

2

Rubyからプロセスをフォークせずに、system()などでPythonを呼び出す方法はないと思います。言語の実行時間はまったく異なり、とにかく別々のプロセスにする必要があります。

+0

サブプロセスモジュールを介してプロセスを呼び出します。 system()にはたくさんの欠点があります。獣を殺しましょう。 – nosklo

6

ルビーにpythonを埋め込むためにMasaki Fukushima's libraryを試すことができますが、それは維持されていないようです。 YMMV

このライブラリでは、Rubyスクリプトは任意のPythonモジュールを直接呼び出すことができます。拡張モジュールとPythonで書かれたモジュールの両方を使うことができます。

ラッキースティッフも使用であるかもしれないのはなぜ面白いこと独創的なからUnholyの名前:

コンパイルのRuby、Pythonのバイトコードに。
そして、加えて、バックDecompyleを使用してPythonのソースコード への
バイトコードを変換する(含まれていた。)

は、Ruby 1.9とPython 2.5が必要です。

+0

ニース。以前は神聖ではないと聞いていなかった。 –

-1

インタープリタを実行するためのPythonコードは、プロセスとして起動する必要があります。だからsystem()はあなたの最善の選択肢です。

RPCまたはネットワークソケットを使用することができるpythonコードを呼び出すために、おそらく動作する可能性のある最も単純なものがあります。あなたはPythonスクリプトのようにPythonのコードを使用したい場合はrubypython

rubypython home page

+0

偉大な、それが私がやろうとしていることです。 system()だけを使うよりも、好きになる理由はありません。 – Eric

+2

私はそれが本当であるとは思わない:別のアプリケーションにPythonインタープリタを埋め込むためのドキュメントについては、http://docs.python.org/extending/embedding.htmlを参照してください。それは、邪悪のために、Rubyの通訳になる可能性があります。 –

6

宝石をインストールする機能である、IO.popenを試してみてください。

pythonスクリプト "reverse.py"を使用して配列内の各文字列を逆順にする場合は、ルビコードは次のようになります。

strings = ["hello", "my", "name", "is", "jimmy"] 
#IO.popen: 1st arg is exactly what you would type into the command line to execute your python script. 
#(You can do this for non-python scripts as well.) 
pythonPortal = IO.popen("python reverse.py", "w+") 
pythonPortal.puts strings #anything you puts will be available to your python script from stdin 
pythonPortal.close_write 

reversed = [] 
temp = pythonPortal.gets #everything your python script writes to stdout (usually using 'print') will be available using gets 
while temp!= nil 
    reversed<<temp 
    temp = pythonPortal.gets 
end 

puts reversed 

その後、あなたのPythonスクリプトは、この

import sys 

def reverse(str): 
    return str[::-1] 

temp = sys.stdin.readlines() #Everything your ruby programs "puts" is available to python through stdin 
for item in temp: 
    print reverse(item[:-1]) #Everything your python script "prints" to stdout is available to the ruby script through .gets 
    #[:-1] to not include the newline at the end, puts "hello" passes "hello\n" to the python script 

出力のようなものになります。 YM olleh EMAN SI ymmij

0

+2

著者は、PythonのRubyコードではなく、RubyからPythonコードを呼び出そうとしています。 –

関連する問題