I'm trying to use the RubyPython module (https://github.com/halostatue/rubypython) to execute Python code from within a Ruby script. I have the module all set up correctly, but am very confused how to use it.
If I had a block of Python code as text, say:
def multiply(x, y):
return x * y * y
z = multiply(x, y)
How would I be able to pass this to Python to execute with 'x' and 'y' defined dynamically in Ruby, and then be able to retrieve the value of 'z'?
Edit per comment request So far, this works and makes sense to me:
RubyPython.start(python_exe: "/usr/bin/python2.6")
cPickle = RubyPython.import("cPickle")
p cPickle.dumps("Testing RubyPython.").rubify
RubyPython.stop # stop the Python interpreter
That gives me an output of "S'Testing RubyPython.'\n."
And I can run very simple commands like this:
RubyPython.start(python_exe: "/usr/bin/python2.6")
x = 3
y = x * x * x
print "y = %d" % y
RubyPython.stop # stop the Python interpreter
That gives me an output of "y = 27"
, as expected.
But once I try to define a method in python, I just get a series of errors:
RubyPython.start(python_exe: "/usr/bin/python2.6")
def my_multiply(x, y):
return x * y * y
z = my_multiply(2, 3)
print "z = %d" % z
RubyPython.stop # stop the Python interpreter
I get syntax error, unexpected ':'
So how would I execute this block of python code using this module? And more importantly, how would I pass values in from Ruby into the Python code that is executing?