tags:

views:

120

answers:

6

Does anyone know of a (preferably open source) PHP to ruby compiler? i.e. a program which parses PHP code and produces semantically equivalent ruby code?

+2  A: 

I don't think one exists.

A combination of Quercus, which re-implements PHP in Java, and JRuby might be of help, depending on what you are trying to accomplish.

http://www.caucho.com/resin-3.0/quercus/

http://jruby.org/

bobdiaes
Interesting idea, but that sounds like more a way to run PHP code together with Ruby code on the JRuby runtime - not a bad idea as the production Quercus compiler is not open source and JRuby seems to be leading the way in terms of non-Java languages on the JVM.
Ramon
+2  A: 

The human brain in tandem with a copy of the Pick-Axe ;)

Chris Mowforth
+2  A: 

This is a pretty daunting task already. And you picked two languages that are very dissimilar. Sure, you could probably programatically translate PHP to Ruby, but the resulting code would be very un-ruby-like.

See some related questions and their answers:

If you could translate idiomatic PHP to idiomatic Ruby you probably need human-like intelligence - ie: the ability to understand what code does (in essence) and rewrite it in the target language. Unfortunately we don't have very smart AI in this front. At least not that I'm aware of.

NullUserException
They are more similar than, say PHP and C++ and people have developed compilers for PHP that target C++, albeit for a different purpose - Facebook's HipHop does this I believe. Syntactically, yes, they are totally different which is why you would need a full-blown compiler and not some sort of "transliterator".
Ramon
@Ramon PHP is written in C after all, and many of its functions are just wrappers to C functions. See the second link for reasons why it would be extremely difficult to create such "compiler."
NullUserException
@Ramon: Compiling PHP into Ruby would likely be an effort similar to that of compiling into C++, with the added work of implementing all those C libraries on which PHP draws (the C++ implementation just uses those C libraries). So, if you want to expend the same amount of energy as Facebook did to get a PHP to Ruby compiler, I'm sure you could do it. You might find out that's quite a lot of work.
Ira Baxter
A: 

http://railsforphp.com/reference/ can be a handy reference when you're just getting started going from PHP to Ruby. You can look up a PHP function and find its Ruby equivalent.

rspeicher
A: 

I haven't used it myself, but you may want to look at Phuby. It allows you to run PHP code within ruby. Then I'd try to unit test the life out of the code (using ruby unit tests), and then re-write the code in ruby.

However, I suspect Phuby isn't even remotely production-ready.

Andrew Grimm
A: 

Stick to one language why do you need converters? just suffer it out and convert that damn php code into ruby visa versa, unless you are planning to make a service? then good luck.

SSpoke
-1 for incoherence. Also, what does "visa versa" mean?
Andrew Grimm
google it, thanks for -1.
SSpoke
@SSpoke: If you googled it, you'd realise that you spelt it wrong.
Andrew Grimm