kin*_*all 14
本文提供了一些从Python运行Ruby代码的技术,这些技术也应该适用于相反的方向(例如XML-RPC或管道)以及从Ruby运行Python代码的特定技术.特别是rubypython或Ruby/Python看起来像他们可能做你想要的.
听起来你会想要使用类似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
的对象等.
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 的类型转换。