tags:

views:

401

answers:

6

Would it be possible to integrate Python (and/or Perl) and Ruby? I've looked at http://www.goto.info.waseda.ac.jp/~fukusima/ruby/python/doc/ and http://code.google.com/p/ruby-perl/ , but they both seem rather outdated.

Has someone generated a Ruby interface for Python's C API?

Edit: Python can be integrated with many other languages according to http://wiki.python.org/moin/IntegratingPythonWithOtherLanguages . However, that list doesn't include Ruby.

+1  A: 

It might by possible, but not very practical. It would be significantly easier to port whatever modules you need from one to the other than it would be to embed one of the interpreters within the other.

If you absolutely have to use both languages in a project, your best option would probably the compination of Jython and Jruby, or IronPython and IronRuby. I'm not sure if you could get them to talk to each other, but at the very least you could host them on the same virtual machine.

mikerobi
+2  A: 

For a research project I wanted to use the fabulous matplotlib that's available for Python. I also found that library that you referred to. However, it doesn't look like something popular and well tested. So I decided to write the script that generated graphs using pure Python and called it from Ruby via popen. That worked very well for me.

ajmurmann
I was looking for something more like Masaki Fukushima's Ruby/Python project, but that is a good idea. Thank you.
Yktula
+6  A: 

My school (Georgia Tech), along with Bryn Mawr and Microsoft Research, are doing a project right now called Pyjama. Basically, it uses the Microsoft DLR to allow you to freely mix Python and Ruby. I haven't tried it, but it sounds pretty cool.

Here's an example from the website. You enter the class in "Python mode". Then it gets compiled, and you run the command in "Ruby mode".

class PythonClass:
     def hello(self, value):
         print "Python says hello to", value

pc = python_class().new
pc.hello "Ruby"

Which produces "Python says hello to Ruby".

Chris Long
That's interesting. I had heard Microsoft's DLR and Mono were both capable of cross-language interoperability, but I hadn't seen a project that uses it. Thank you.
Yktula
+2  A: 

You can write extensions for Ruby in C.

So, if Python has a C API, you can write a C extension for Ruby which uses this API.

I know nothing about the Python API or how large of a piece you want to integrate with, but if it is not too big, this could (possibly) give you a way to run Python code from Ruby.

Pete
To use C extension for Ruby you must use Ruby FFI project — http://github.com/ffi/ffi
Aleksandr Koss
ffi is not a requirement, but it does make integration with other non MRI ruby implementations possible.
Mark Carey
It's much more straightforward to use than Ruby/DL. I want to try to do this later with Python's C API. Thank you.
Yktula
+2  A: 

Integrating dynamic languages is one of the goals of the Parrot project. It's a virtual machine that dynamic language compilers target. Once compiled to the same virtual machine, you should be able to used the "object" form in any of the languages no matter the object's source.

The issue at the moment, however, is stabilizing the virtual machine and finishing off the mostly done compilers. However, that's been the state for a long time. :)

brian d foy
+1  A: 

Another strategy, as used by Facebook, is to expose APIs via Thrift. You define lightweight service APIs and the RPCs are inter-process.

Scott Robinson