31

I have a compiled Python library and API docs that I would like to use from Ruby.

Is it possible to load a Python library, instantiate a class defined in it and call methods on that object from Ruby?

6 Answers 6

14

This article gives some techniques for running Ruby code from Python which should also be applicable in the reverse direction (such as XML-RPC or pipes) as well as specific techniques for running Python code from Ruby. In particular rubypython or Ruby/Python look like they may do what you want.

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

6 Comments

Do you know of any available comparisons between these 2 offerings?
Sadly, no. Nor have I used either myself.
I asked the Ruby/Python author about the project and he said it is old and unmaintained, so it might be best for you to go with ruby python.
rubypython hasn't had an update since 2012, explicitly doesn't support python3, and i'd bet money it needs updates for ruby 2.x -- it looks like there's no in-process solutions and some form of IPC is the only way to go.
Pycall might do the trick and is maintained in 2024
|
8

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

Here is a simple example to call Python's math.sin function and compare it to the Math.sin in Ruby:

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

Type conversions from Ruby to Python are automatically performed for numeric, boolean, string, arrays, and hashes.

Comments

5

It sounds like you would want to use something like Apache Thrift which allows either your python or your ruby code to be a server/client and call each other. http://thrift.apache.org/

You can instantiate your objects in ruby and or in python based on your thrift definition. This is an example from the thrift website.

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

Basically your ruby or python will be able to call store() and retrieve() and create UserProfile objects etc.

1 Comment

Wow. This is pretty awesome, and ingenious! It's basically wrapping up your code and serving them like an API. Thanks for sharing!
3

Even you can make this work, you might want to consider if this is the best architectural choice. You could run into all sorts of versioning hell trying to maintain such a beast.

If you really can't find an equivalent Ruby library (or it's a big investment in Python you want to leverage,) consider using a queue (like RabbitMQ) to implement a message passing design. Then you can keep your Python bits Python and your Ruby bits Ruby and not try to maintain a Frankenstein build environment.

1 Comment

The Python library is an interface to a piece of commercial test equipment, so I don't have the option of changing it or rewriting in Ruby.
3

This little library makes it super easy to do this: https://github.com/steeve/rupy

Comments

0

Are you on Windows? Could you create a DLL or COM object from your python lib and call it with Ruby's Win32Api or Win32Ole?

Comments

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.