top | item 2498755

Fructose: compile Ruby into PHP

127 points| cygwin98 | 15 years ago |fructoselang.org | reply

52 comments

order
[+] jameskilton|15 years ago|reply
What makes this project even more incredible is the fact that it's written in C# under .NET 4.0 using IronRuby for the parser.

Yes it says Mono is supported, but why? Windows is the least run environment for either of these languages (Ruby / PHP). Were I to guess, I'd say this was a dare that it couldn't be done, and if that's the case, then hats off to the owner for doing it.

[+] aphyr|15 years ago|reply
Upvoting for sheer perversity.
[+] wiredfool|15 years ago|reply
Well, this is only half the battle. The rest of it should be that it's running on a stack of apple //e motherboards.

Now that would be perverse.

[+] VladRussian|15 years ago|reply
seeing the PHP monstrosity Ruby has as an equivalent straightens my feelings of what an ugly language Ruby in it intrinsic nature is
[+] mnutt|15 years ago|reply
Somewhat related: One RubyConf, zenspider and tenderlove hacked together Phuby on Phails, an abomination that allowed you to use PHP within Rails. It even translated instance variables from Rails controllers to PHP views.

http://www.youtube.com/watch?v=lsWKjS6Vufw

[+] bonquesha99|15 years ago|reply
Projects like these are awesome learning experiences. I've been working on something similar (rubyisms in php) but in pure php - https://github.com/shuber/phuby

  * mixins
  * classes are objects
  * eigenclasses
  * method_missing, respond_to, respond_to_missing, send, super
  * splat
  * extended, included, inherited callbacks
Definitely made me appreciate ruby more after working on it.
[+] elliottcarlson|15 years ago|reply
Besides being a fun side project and having a chance to play with lexical parsing - does this have any point?

Converting programming languages really doesn't serve any purpose whatsoever - this can't be used as a learning tool (a rubyist wanting to learn php) nor should this output ever be trusted in a production environment as you are going to count on the converted code being as efficient as your original code.

If you want to code in PHP, use PHP - if you want to code in Ruby, use Ruby - this just seems silly.

[+] tlrobinson|15 years ago|reply
There are plenty of reasons to use source-to-source compilers (which is what I assume you meant by "converting programming languages")

* Running multiple languages (each with it's own strengths) on platforms that natively support only a few languages. e.x. GWT, CoffeeScript.

* Enhancing an existing language. e.x. Objective-J, Stratified JavaScript.

* Performance. e.x. HipHop for PHP.

* Quickly building new languages. Objective-C (and I think C++?) was originally just a preprocessor for C.

That said, I personally don't find a Ruby-to-PHP compiler particularly useful. Maybe it could be useful for running Ruby on the ubiquitous and cheap Apache/PHP shared hosts, but I suspect the overhead of loading a large amount of code (even just Ruby's standard library, let alone Rails) on each request will make that infeasible.

[+] jrockway|15 years ago|reply
Converting programming languages really doesn't serve any purpose whatsoever - this can't be used as a learning tool (a rubyist wanting to learn php) nor should this output ever be trusted in a production environment as you are going to count on the converted code being as efficient as your original code.

I'm guessing you don't use a lot of programs written in C?

[+] pkulak|15 years ago|reply
C++ started out as a precompiled language, and I love CoffeeScript, which compiles to JavaScript. Java and C# don't compile all the way down to assembly either. There are plenty of reasons to compile one language to another.
[+] rtaycher|15 years ago|reply
"Converting programming languages really doesn't serve any purpose whatsoever" Compilers are useful. This probably won't reach production quality but that doesn't mean it couldn't with the right effort, see hip-hop or one of those x-> javascript compilers.
[+] drm237|15 years ago|reply
The only reason this seems at all useful would be to take advantage of cheap shared hosting that can only run php. But that's hardly a good reason given that you can run a ruby (rack-based) app on heroku for free!
[+] mburney|15 years ago|reply
I've had consulting gigs where I was forced to write PHP apps because of management decisions. Something like this would have been helpful.
[+] stanmancan|15 years ago|reply
Thats a pretty crappy example to put on the homepage: 13 lines of Ruby compiled into 67 lines of PHP. You could write some PHP more or less identical to the Ruby in the same original 13 lines.
[+] ctide|15 years ago|reply
I imagine the example is pretty indicative of how it works. Better to have a crappy example than no example at all.
[+] fosk|15 years ago|reply
Actually most of the code initializes Fructose, in PHP I can write the same code like (6 lines, 7 including blank line):

  class Greeter {
      public static function greet($text) {
          echo $text;
      }
  }

  Greeter::greet("Hello, World!\n");
[+] hardboiled|15 years ago|reply
A call of action to all PHP developers out there: We must defend our language against this misrepresentation that PHP is ugly and verbose. It is not a fair comparison of teh power of PHP as it is now. I suggest we start a similar project called "Sucralose" in which we compile PHP into Ruby 1.8 written in Objective-C using MacRuby. I will host it on PEAR. All hail Zend!
[+] compay|15 years ago|reply
Tip of the cap for the fun hack factor. People asking if this is practical are missing the point, I think.
[+] bigiain|15 years ago|reply
+1

I like the implications of the name. Fructose - it's kinda sweet but not quite real sugar, and there's a suspicion of some deep and not-quite-right subterfuge going on underneath the entire industry that supports it for no real reason...

[+] mitchellh|15 years ago|reply
Although I'm sure this was a fun weekend project, which is very cool, I can say that I could actually see something like this being useful IF Ruby could be compiled to PHP which could then be compiled by HipHop to C++.

But still, fun project to see.

[+] jonursenbach|15 years ago|reply
How on earth could that be useful? Compiling a piece of Ruby three times down into C, you're going to end up a with an absolute pile of shit.
[+] erik_p|15 years ago|reply
has anyone created the opposite? Compile PHP into Ruby?

(Did I just feel all of the HN rubyists shudder at once?)

[+] StavrosK|15 years ago|reply
Hell, I'm a Pythonista and I shuddered nonetheless.
[+] swivelmaster|15 years ago|reply
I've actually played around with this concept. Conceptually it's not very difficult (or at least it wasn't for the code I was working with)... The biggest issue is PHP's reliance on global functions vs Ruby's methods for strings, numbers, arrays, etc. So, for every PHP global string function, for instance, you'd need to pull out one of the arguments and call an equivalent method on it. Bleh.
[+] rauljara|15 years ago|reply
Is this some sort of snide commentary on how much more limited of a language php is? It doesn't seem like it could possibly be meant for production. I guess I could see it being a fun learning project, tho.
[+] healthyhippo|15 years ago|reply
I just sent this out to a friend who was trying to figure out whether to build his simple app in ruby vs. php. Think it made up his mind.
[+] TamDenholm|15 years ago|reply
Is there anything that can do the reverse?
[+] mduvall|15 years ago|reply
I'm curious - would it be inefficient to use something that could efficiently compile Ruby into PHP such as Fructose, and then use HipHop for PHP from Facebook to compile the PHP into C++? So far I don't see a lot of tools that utilize the Ruby->C++ direction, and much work has gone into HipHop for PHP.
[+] lautis|15 years ago|reply
It won't work.

First, HipHop doesn't support create_function, which is used in the example.

Second, feature mismatch between Ruby and PHP will make efficient compilation quite hard and most likely be far from optimal. The languages doesn't seem to be very easily optimizable either.

HipHop isn't a silver bullet either: sometimes it seems to be faster than cached PHP bytecode, but not always.