从Ruby调用Python

Bry*_*Ash 28 ruby python

我有一个编译的Python库和API文档,我想从Ruby中使用它.

是否可以加载Python库,实例化其中定义的类并从Ruby调用该对象的方法?

kin*_*all 14

本文提供了一些从Python运行Ruby代码的技术,这些技术也应该适用于相反的方向(例如XML-RPC或管道)以及从Ruby运行Python代码的特定技术.特别是rubypythonRuby/Python看起来像他们可能做你想要的.

  • 我向Ruby/Python作者询问了这个项目,他说它已经老了,没有维护,所以最好还是选择ruby python. (3认同)

Mar*_*tin 5

听起来你会想要使用类似Apache Thrift的东西,它允许你的python或你的ruby代码成为服务器/客户端并相互调用. http://thrift.apache.org/

您可以根据您的thrift定义在ruby和/或python中实例化对象.这是节俭网站的一个例子.

struct UserProfile {
    1: i32 uid,
    2: string name,
    3: string blurb
  }
  service UserStorage {
    void store(1: UserProfile user),
    UserProfile retrieve(1: i32 uid)
  }
Run Code Online (Sandbox Code Playgroud)

基本上,你的Ruby或Python将能够调用store()retrieve()创造UserProfile的对象等.


Hap*_*ace 5

https://github.com/mrkn/pycall.rb

下面是一个调用 Python 的 math.sin 函数并将其与 Ruby 中的 Math.sin 进行比较的简单示例:

require 'pycall/import'
include PyCall::Import
pyimport :math
math.sin(math.pi / 4) - Math.sin(Math::PI / 4)   # => 0.0
Run Code Online (Sandbox Code Playgroud)

数值、布尔值、字符串、数组和哈希值会自动执行从 Ruby 到 Python 的类型转换。