Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compile Ruby to Javascript? [closed]

Tags:

I'm working on a piece of logic that I would like to express on the server as well as in the browser. Something like validating a form where there must be certain logical relationships between the elements based on what has already been entered.

So... If I can write the logic once and somehow end up with both Ruby and with Javascript, I can write the logic just once and not have to worry about making sure that two pieces of code written in different languages have the same functional behaviour.

I don't need to reproduce everything in Ruby, obviously, and one simplification might be to port a single general-purpose library like Functional Javascript to Ruby.

Does anyone have experience with RubyJS? Can anyone point me to an existing project using RubyJS? Thanks in advance...

like image 628
raganwald Avatar asked Jan 27 '10 02:01

raganwald


People also ask

Does Ruby compile to Javascript?

Short answer: yes. You can use direct Ruby to compile to Javascript using Opal .

Can you compile Ruby?

The simple answer is that you can't, at least with MRI 1.8 (the standard). This is because 1.8 works by walking the Abstract Syntax Tree. Python, Ruby 1.9, JRuby, and Rubinius use byte code, which allows compilation to an Intermediate Representation (byte code).


2 Answers

As far as I can tell, Opal is the best Ruby to JS converter/compiler out there right now. Here you can see it in action.

It isn't perfect, but it works most of the time and unlike older projects such as RubyJS, Opal is still being actively developed.

like image 109
Ajedi32 Avatar answered Sep 18 '22 15:09

Ajedi32


I know about http://hotruby.yukoba.jp/ but have never used it

like image 28
Matthew FL Avatar answered Sep 21 '22 15:09

Matthew FL