October 1st, 2016, 03:14 AM
can someone point me to where/how i would go about calling a ruby app from a
python app, and having the python app being able to get a returned value
from the ruby script.
a = os.exec(testruby.rb)
foo = 9
i know this doesn't work... but i've been searching for hours on this with
no luck.... (and yeah, i'm relatively new to both ruby/python!!)
October 10th, 2016, 10:16 AM
I'd use a pipe from python and have ruby return a string which python reads and parses. Communication via a named file is a another option.
with os.popen('testruby.rb', 'rt') as ruby_output_file:
lines = ruby_output_file.readlines()#lines is a list of lines written by the testruby.rb executable program
completely untested. These are the most likely issues I'd expect to arise were I to test, and there are a lot of potential problems:
- I may have confused input and output.
- The lines might be some bytes type rather than str type, which might vary if you depending on python 2 and 3.
- os.popen might not have a context manager, which also might depend on python version
[/code] are essential for python code and Makefiles!
December 22nd, 2016, 02:49 AM
Solution 1: run a Ruby script via os.system() or subprocess.Popen()
This is the simplest solution: just run the Ruby script as an external process, using either os.system() or subprocess.Popen(). The major drawback is that it launches a new Ruby interpreter at each call, so performance is very poor if you need to call it several times.
Solution 2: translate Ruby code to Python
Of course the best solution would be to have an automated translation from one language to the other. For now I haven't found any tool for this. Please leave a comment if you know one.
However, a promising alternative is proposed by why: instead on attempting to translate source code, unholy (http://github.com/whymirror/unholy) is able to translate Ruby bytecode to Python bytecode.
Solution 3: use XML-RPC
Both Python and Ruby support XML-RPC natively, so this is an interesting solution to call Ruby functions and classes directly from Python. XML-RPC handles simple data types natively, however more complex classes have to be translated into standard objects. Moreover, the Ruby script must run has a server providing an XML-RPC interface for the Python script running as a client. There are also a few security issues because a XML-RPC server is available to any other process, and exchanged data may be sniffed (unless you use HTTPS and authentication). Finally, XML-RPC is based on XML and HTTP so this may be too much overhead unless the Ruby code is not called too often.