繁体   English   中英

如何在Ruby中运行Python代码?

[英]How can I run Python code in Ruby?

我想将我的Python代码放入Ruby模块中并运行它,但是我不知道如何将Python独立脚本导入Ruby。 以下是我的代码:

Python代码

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

红宝石

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

问题是在RubyPython.startRubyPython.stop之间,我不知道如何导入Python文件。 另一个问题是在该模块中运行Ruby的代码是什么?

这是一种完全过分,过于复杂的方法,但是无论如何这都是这样。

假设您的python模块(文件)名为googstock.py 它看起来应该像这样:

import ystockquote

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

然后您的Ruby可能如下所示:

RubyPython.start

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

RubyPython.stop

不过,在将来,请尝试仅从Ruby中的Yahoo! API获取库存值。 没那么难。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM