1

I am using python, and I want to call a Ruby function (i.e. in a similar manner to how i would call a python function), specifying function arguments, and then get back a returned value.

I have been searching around, and can see how to run ruby code e.g. using os.system, however can't seem to either:

  1. call a specific function within the Ruby code, passing variables
  2. Return the result of the function to Python.

e.g. I am looking to be able to do something similar to:

 desired_response = ruby_function(variables)

where ruby_function is within a file called ruby_file, and the response is the result of running that function.

2
  • for reference, while there may have been more elegant ways of doing this, i ended up saving the function variables in a text file, calling the ruby file, which extracted the variables from the text file, and then saved it's output in another text file, which Python read back in. Commented Mar 24, 2015 at 23:01
  • Some thoughts: make the ruby script itself return the value and then use subprocess.check_output; wrap the ruby function in a microservice using sinatra; use JRuby and jython together (not so sure about this one...) Commented Mar 25, 2015 at 1:30

2 Answers 2

0

I don't know what is your output from ruby program but with os.startfile()you can run your ruby script.

Sign up to request clarification or add additional context in comments.

2 Comments

My output is a string, and I am looking to get that string back into python
also this link in stackoverflow: link
0

Here's a more recent solution that i found and seems a lot smoother than having a third party tool to invoke this.

You can simply use the Python subprocess. I ended up using it this way:

import subprocess response = subprocess.run([f"{os.environ['YOUR_SCRIPT_PATH']}", first_variable_to_pass_to_script, second_variable])

Hope this helps!

###OLD ANSWER: I found this article to be helpful! http://sweetme.at/2014/03/14/a-simple-approach-to-execute-ruby-scripts-with-python/#:~:text=Execute%20the%20Ruby%20File%20from%20Naked.toolshed.shell%20import%20execute_rb,the%20Ruby%20code%20to%20the%20Python%20calling%20code.

You might need to use:

from Naked.toolshed.shell import execute_rb success = execute_rb('testscript.rb')

to be able to handle your standard I/O error in Python rather than Ruby.

Original documentation: https://naked.readthedocs.io/toolshed_shell.html

Comments

Your Answer

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.