7 ms·
Fructose: compile Ruby into PHP
- deleted 15y ago[deleted]
- rauljara 15y agoIs 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 15y agoI 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.
- mfjordvald 15y agoThat's a pretty stupid reason, though. The two samples of code are no where near identical...
- jasonlotito 15y ago> That's a pretty stupid reason, though. Yes. So, do you really want him writing PHP?
- eropple 15y agoI don't want him writing Ruby, either. Keep him away from the sharp knives and stupid decisions and give him Java/Play or something.
- MattBearman 15y agoI'm assuming his decision was ruby based on the ugly PHP code in the example? If so, your friend is wrong for two reasons: 1 - Writing the example in PHP from scratch would be at least as simple as writing it in Ruby, it's only because it's been converted that the PHP code is so convoluted. 2 - Depending on what you consider a to be 'small' app, PHP is probably the better language. I find Ruby is more suited to larger scale apps, especially where persistence is advantageous. In short, Fructose is just going to give PHP more undeserved bad press.
- deleted 15y ago[deleted]
- deleted 15y ago[deleted]
- drm237 15y agoThe 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 15y agoI've had consulting gigs where I was forced to write PHP apps because of management decisions. Something like this would have been helpful.
- mdasen 15y agoI'm guessing that your clients wouldn't be pleased with the PHP the compiler produces. It wouldn't really be maintainable PHP code for them.
- aphyr 15y agoUpvoting for sheer perversity.
- wiredfool 15y agoWell, 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 15y agoseeing the PHP monstrosity Ruby has as an equivalent straightens my feelings of what an ugly language Ruby in it intrinsic nature is
- deleted 15y ago[deleted]
- jessedhillon 15y agoYou realize that this logic can be applied, in reverse, to make a similar conclusion about PHP, right? Seeing the monstrous PHP code that equates to simple Ruby statements confirms my feelings about what a poorly designed language PHP is. (For example)
- VladRussian 15y agoyes, i understand, Friday, end of the week, the humor storage got depleted somewhere around Wednesday.
- dpkendal 15y agoThis is an unavoidable part of compilation; there's no way a compiler could write code as well as a human. It applies to all compilers; a human programmer writing assembly language would be hard-pressed to understand the output of a C compiler.
- amanelis 15y agototally agree with that statement, look at the massive output in php vs the simple readable rb code
- deleted 15y ago[deleted]
- deleted 15y ago[deleted]
- mitchellh 15y agoAlthough 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 15y agoHow 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.
- amadeus 15y agoSarcasm is lost on this one :)
- jonursenbach 15y agoI don't think the OP was sarcastic.
- amadeus 15y agoHe's sitting right across the table from me :)
- mduvall 15y agoI'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 15y agoIt 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.
- stanmancan 15y agoThats 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 15y agoI imagine the example is pretty indicative of how it works. Better to have a crappy example than no example at all.
- fosk 15y agoActually 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");
- electrum 15y agoThat code isn't the same as the original. The instance variable is being initialized via a block -- your function needs to accept a callback.
- charliesome 15y agoThis would be the PHP that'd do the same thing: class Greeter { public function __construct($fn) { $this->greeting = $fn(); } public function greet() { echo $this->greeting; } } $greeter = new Greeter(function() { return "Hello, World!"; }); $greeter->greet(); That code will only work in PHP 5.3. If you want anonymous functions in lower versions, you're going to have to use create_function(). If you want closures, you may as well forget about doing it nicely. There's a reason the Fructose generated PHP isn't very pretty. The Fructose generated code will run on any version of PHP that's at least 5.0.
- TamDenholm 15y agoIs there anything that can do the reverse?
- bonquesha99 15y agoProjects 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 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.
- jameskilton 15y agoWhat 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.
- erik_p 15y agohas anyone created the opposite? Compile PHP into Ruby? (Did I just feel all of the HN rubyists shudder at once?)
- StavrosK 15y agoHell, I'm a Pythonista and I shuddered nonetheless.
- swivelmaster 15y agoI'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.
- elliottcarlson 15y agoBesides 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.
- jrockway 15y agoConverting 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?
- tlrobinson 15y agoThere 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.
- elliottcarlson 15y agoRunning multiple languages (each with it's own strengths) on platforms that natively support only a few languages. e.x. GWT, CoffeeScript. While this may be a valid arguement for some - I just can't accept it - if you are targeting a specific platform you really should (completely imho) use the tools meant for it. Anything else will easily lead to issues that may not be debug-able if you aren't already familiar with your target. Enhancing an existing language. e.x. Objective-J, Stratified JavaScript. Both of these enhance an existing language by creating a interpreted scripting language that gets compiled down by the original language. I wouldn't classify these as a source-to-source compiler imho. Performance. e.x. HipHop for PHP. HipHop is indeed a good example and I stand corrected there. The slight difference is that this is specifically targetted at creating highly optimized C++ to be compiled for quicker execution. In a Ruby-to-PHP example it's certainly not going to add performance gain and is a silly idea - but as I said, I didn't think about a valid execution like HipHop in my initial blanket statement. Quickly building new languages. Objective-C (and I think C++?) was originally just a preprocessor for C. I didn't even think about preprocessors and they are indeed another valid implementation for source-to-source compilation. My original statement was a bit too quickly posted - thanks for the correction!
- compay 15y agoTip of the cap for the fun hack factor. People asking if this is practical are missing the point, I think.
- bigiain 15y ago+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...
- alexg0 15y agoWould be cool if it was self-hosted, written in Ruby, but ran in complied to PHP form.
- deleted 15y ago[deleted]
- michaelhalligan 15y agoLooking at the PHP this generates, is it just generating excessively bad PHP, or does it really take 4x as much code to do anything in PHP?
- ashchristopher 15y agohttps://github.com/charliesome/Fructose/pull/9 https://github.com/charliesome/Fructose/pull/9 Active community participation too =).
- mnutt 15y agoSomewhat 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 http://www.youtube.com/watch?v=lsWKjS6Vufw
- hardboiled 15y agoA 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!