user2668789
user2668789

Reputation: 211

How can I run Python code in Ruby?

I want to put my Python code in Ruby Module and run it, but I don't know how to import the Python stand-alone script into Ruby. The following are my code:

Python code

import ystockquote
import pprint
pprint.pprint(ystockquote.get_all('GOOG'))

Ruby

module PythonInRuby
require "rubypython"

RubyPython.start # start the Python interpreter

cPickle = RubyPython.import("cPickle")
p cPickle.dumps("Testing RubyPython.").rubify

RubyPython.stop # stop the Python interpreter

The problem is that between the RubyPython.start and RubyPython.stop, I don't know how to import the Python file. And another problem is what the code is for running Ruby in this module?

Upvotes: 1

Views: 1877

Answers (1)

Linuxios
Linuxios

Reputation: 35783

This is a completely overdone, over-complicated way to do this, but here's how anyway.

Say your python module (file) is named googstock.py. It should look like this:

import ystockquote

def run():
    return ystockquote.get_all('GOOG')

Then your Ruby can look like this:

RubyPython.start

googstock = RubyPython.import('googstock')
puts googstock.run().rubify #Get the stock data into Ruby

RubyPython.stop

In the future though, try just getting the stock values from Yahoo!'s API in Ruby. It can't be that hard.

Upvotes: 2

Related Questions