1

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?

4
  • 4
    This is likely the most complicated and brittle way to solve this issue possible. Why do you want this? Commented Aug 16, 2013 at 14:54
  • Because I don't know how to get financial data using Ruby language. Commented Aug 16, 2013 at 14:56
  • 2
    Make a script in Python to get the financial data, and call it with a subprocess call from Ruby. Much easier. Commented Aug 16, 2013 at 14:57
  • 2
    So, I guess the next logical question is, if you know how to do it in Python, why do you need to use Ruby? Commented Aug 16, 2013 at 14:58

1 Answer 1

2

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.

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

2 Comments

Thanks so much, by the way, how can I run this ruby module on rails?
@user2668789: Depends where you're hosting the code. But for that, I suggest calling the API directly. This is not production ready, and for something so simple converting really is an option.

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.