Automatic Ruby to JavaScript conversion

Here’s something for the hungry mob to tear apart: automatic Ruby to
JavaScript conversion. It seems somehow wrong, like grafting a pretty
girl’s head onto a donkey, but I’ve done it anyway!

It converts Ruby code like this:


class Demo

def initialize
@clicks = 0
3.times{ self.puts(‘Hello! I am a Ruby script!’) }
end

def puts(str)
document.getElementById(‘debug’)[‘innerHTML’] =
document.getElementById(‘debug’)[‘innerHTML’] + str + “\n”
end

def clicked
@clicks += 1
self.puts("Click number " + @clicks.to_s)
end

end


into JavaScript code like this:


function Demo(){
self=this;
self.instanceVariables={};
self.instanceVariables[‘@clicks’]=Number(0);
Number(3).times(function(){
self.puts(“Hello! I am a Ruby script!”)
})
}
Demo.prototype = {
puts: function(str){
self=this;
document.getElementById(“debug”)[“innerHTML”]=document.getElementById(“debug”)[“innerHTML”]+str+“\n”
},
clicked: function(){
self=this;
self.instanceVariables[‘@clicks’]=self.instanceVariables[‘@clicks’]+Number(1);
self.puts("Click number
"+self.instanceVariables[‘@clicks’].toString())
}
}


It uses Ryan D. et al’s ParseTree and Florian GroÃ?'s ruby.js for
most of the hard work. It’s still very limited, and there are many
warts, not least of which is the requirement for an explicit receiver
on every method.

That said, the code’s here: http://po-ru.com/files/ruby2js.tar.gz

Have fun!
Paul.

On Jul 5, 2006, at 11:30 AM, Paul B. wrote:

That said, the code’s here: http://po-ru.com/files/ruby2js.tar.gz

Have fun!
Paul.

Grotesquely awesome! I especially like the unsettling analogy of the
pretty-faced donkey.

Now what would be a great addition to that would be a mod_ruby or
rails plugin that allowed .rjs files to be automagically translated
from ruby to javascript.
-Mat

+1

I’ve had grand visions for where this kind of thing could go (think
google’s GWT). If only I had time…

Nice work, I hope you continue developing the idea. This could really
take rjs to another level.

Dan Amelang

Holy cow. I can see something like this going far. Keep it up man!

  • Jake McArthur

On Jul 5, 2006, at 8:30 AM, Paul B. wrote:

Here’s something for the hungry mob to tear apart: automatic Ruby to
JavaScript conversion. It seems somehow wrong, like grafting a pretty
girl’s head onto a donkey, but I’ve done it anyway!

Personally, I find JS a pretty awesome language, but I don’t write JS
the typical way.

It uses Ryan D. et al’s ParseTree and Florian Groß’s ruby.js for
most of the hard work. It’s still very limited, and there are many
warts, not least of which is the requirement for an explicit receiver
on every method.

Why aren’t you using SexpProcessor? It takes care of all the stuff
you’ve got in #handle and more! Also makes it easy to write tests.

You can probably handle :fcall if you have the correct class
hierarchy and just use self.


Eric H. - [email protected] - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

On 05/07/06, Eric H. [email protected] wrote:

Why aren’t you using SexpProcessor? It takes care of all the stuff
you’ve got in #handle and more! Also makes it easy to write tests.

Good question - the answer is simply that I wasn’t aware of it! It
didn’t take very much work to get to this point - it’s just a quick
and dirty hack (with no tests :slight_smile: to see if the idea is viable. It
seems that it is.

You can probably handle :fcall if you have the correct class
hierarchy and just use self.

Yeah. I took the easy route, but it should be possible with a bit of
work.

Thanks for the insights.

Paul.

On Jul 5, 2006, at 1:55 PM, Paul B. wrote:

On 05/07/06, Eric H. [email protected] wrote:

Why aren’t you using SexpProcessor? It takes care of all the stuff
you’ve got in #handle and more! Also makes it easy to write tests.

Good question - the answer is simply that I wasn’t aware of it! It
didn’t take very much work to get to this point - it’s just a quick
and dirty hack (with no tests :slight_smile: to see if the idea is viable. It
seems that it is.

It should be easy to convert to SexpProcessor. I’d be happy to give
you any pointers if you get stuck, just email me privately.

You can probably handle :fcall if you have the correct class
hierarchy and just use self.

Yeah. I took the easy route, but it should be possible with a bit
of work.

I fear it may be more than “a bit” if you want an exact match…


Eric H. - [email protected] - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com