0

I have a ruby script that gets executed by a python script. From within the python script I want to access to return value of the ruby function.

Imagine, I would have this ruby script test.rb:

class TestClass
 def self.test_function(some_var)
 if case1
 puts "This may take some time"
 # something is done here with some_var
 puts "Finished"
 else
 # just do something short with some_var
 end
 return some_var
 end
end

Now, I want to get the return value of that function into my python script, the printed output should go to stdout.

I tried the following (example 1):

from subprocess import call
answer = call(["ruby", "-r", "test.rb", "-e", "puts TestClass.test_function('some meaningful text')"])

However, this gives me the whole output on stdout and answer is just the exit code.

Therefore i tried this (example 2):

from subprocess import check_output
answer = check_output(["ruby", "-r", "test.rb", "-e", "puts TestClass.test_function('some meaningful text')"])

This gives me the return value of the function in the else case (see test.rb) almost immediately. However, if case1 is true, answer contains the whole output, but while running test.rb nothing gets printed.

Is there any way to get the return value of the ruby function and the statements printed to stdout? Ideally, the solution requires no additional modules to install. Furthermore, I can't change the ruby code.

Edit:

Also tried this, but this also gives no output on stdout while running the ruby script (example 3):

import subprocess
process = subprocess.Popen(["ruby", "-r", "test.rb", "-e", "puts TestClass.test_function('some meaningful text')"], stdout=subprocess.PIPE)
answer = process.communicate()

I also think that this is no matter of flushing the output to stdout in the ruby script. Example 1 gives me the output immediately.

asked Apr 25, 2012 at 20:33
1
  • 2
    It has nothing to do with whether the other program is written in Ruby or not. You need to use popen to get the output stream from standard output. Commented Apr 25, 2012 at 20:47

2 Answers 2

1

Another way of doing this, without trying to call the ruby script as an external process is to set up a xmlrpc (or jsonrpc) server with the Ruby script, and call the remote functions from Python jsonrpc client (or xmlrpc)- the value would be available inside the Python program, nad even the sntax used would be just like you were dealing with a normal Python function.

Setting up such a server to expose a couple of functions remotely is very easy in Python, and should be the same from Ruby, but I had never tried it.

answered Apr 26, 2012 at 3:09
Sign up to request clarification or add additional context in comments.

Comments

0

Check out http://docs.python.org/library/subprocess.html#popen-constructor and look into the ruby means of flushing stdout.

answered Apr 25, 2012 at 20:43

2 Comments

If stdout is not flushed when the process exits (wouldn't it be?) you can use either $stdout.sync=true at the top of your script or $stdout.flush at the end.
I tried using process = subprocess.Popen(...) and process.communicate(). But this gives the same results - I get nothing to stdout while the ruby script is running.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.