Is it possible to convert Ruby code to Javascript at all? I have heard of RubyJS but this appears to not work with Ruby 1.9.2 - is this the case?
-
6I'd always be cautious of writing code in one high-level language and then converting to another. Like all code generators, it's unlikely to end up with the best possible code in the target language to do the job. It's almost always better to simply write the javascript code directly.Spudley– Spudley2011-06-02 12:47:07 +00:00Commented Jun 2, 2011 at 12:47
-
Also, you have to consider if the library that your Ruby code uses can be translated directly to JavaScript. More likely, you'll be porting your Ruby code to JavaScript.OnesimusUnbound– OnesimusUnbound2011-06-02 12:54:38 +00:00Commented Jun 2, 2011 at 12:54
4 Answers
It seems to me that Opal is the best Ruby to JavaScript converter/compiler out there right now. You can see it in action here.
It isn't perfect, but it works in most cases and unlike older projects such as RubyJS, Opal is still being actively developed. (Check out Opal on Github)
Comments
There are many efforts that have tried to do this, but most have only been to an academic degree. For instance, this Ruby 1.9.2 VM in JavaScript.
Most efforts to port to JavaScript focus on the Google V8 engine and not necessarily the browser-side equivalent.
2 Comments
Try https://www.ruby2js.com/demo?preset=true Worked fine for me. In case you're using very long scripts and it fails, try smaller chunks of code.