#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2014
    Posts
    40
    Rep Power
    3

    python/ruby question..


    hi...

    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.

    something like

    test.py
    a = os.exec(testruby.rb)


    testruby.py
    foo = 9
    return foo


    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!!)

    thanks
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,785
    Rep Power
    508
    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.

    #! /usr/bin/python3

    import os
    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.
    • Misspelling.
    • 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]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2014
    Posts
    40
    Rep Power
    3
    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.

IMN logo majestic logo threadwatch logo seochat tools logo