Convert Ruby to JavaScript
I knocked together this simple proof-of-concept Ruby-to-JavaScript converter in a few hours today. 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 Davis 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. Nonetheless, I think it’s going to be useful.
Interested? Download it and try it for yourself.
Update
There’s now a project page on RubyForge.
2006-07-05 15:23 UTC. Comments: 16.
Florian Groß
Wrote at 2006-07-05 23:02 UTC using Firefox 1.5.0.4 on Windows XP:
Woah, how very cool!Erik Gregg
Wrote at 2006-07-05 23:16 UTC using Firefox 1.5.0.4 on Linux:
Holy Crap! Insane! This is exactly what I need right now!_ugly
Wrote at 2006-07-05 23:33 UTC using Firefox 1.5.0.1 on Linux:
dude. keep working on this. don’t stop. please.Kris
Wrote at 2006-07-06 09:24 UTC using Firefox 1.5.0.4 on Windows XP:
“It seems somehow wrong, like grafting a pretty girl’s head onto a donkey”lol
Great work, hope you finish it off.
Peter Cooper
Wrote at 2006-07-06 09:37 UTC using Firefox 1.5.0.3 on Mac OS X:
Verrry nice. Are you going to be at Pizza on Rails tonight by any chance? :)Folkestone Gerald
Wrote at 2006-07-06 10:09 UTC using Firefox 1.5.0.1 on Windows XP:
Interesting idea, cheers…Folkestone Gerald
Wrote at 2006-07-06 10:13 UTC using Firefox 1.5.0.1 on Windows XP:
Found you via del.icio.us btwBen
Wrote at 2006-07-06 11:12 UTC using Firefox 1.5.0.4 on Windows XP:
Argh that’s terrible!Tim Lucas
Wrote at 2006-07-06 14:29 UTC using Safari 417.9.3 on Mac OS X:
I see nothing wrong with grafting a pretty girl’s head onto a donkey…masukomi
Wrote at 2006-07-06 14:31 UTC using Firefox 1.5.0.1 on Linux:
I have to agree with _ugly. This could be reeeally useful.Please keep working on it. Maybe throw it on sourceforge or something so that others who are interested migh more easily help.
Paul Battley
Wrote at 2006-07-06 14:42 UTC using Firefox 1.5.0.4 on Mac OS X:
Don’t worry—I intend to keep working on it! I’ve got some good ideas for message handling and so on (it’ll make the resulting JavaScript more complicated, but the behaviour will be closer to Ruby).I’ve submitted a RubyForge project request, and I’ll get everything set up on there once it’s authorised, maybe some time this weekend.
mike
Wrote at 2006-07-06 17:12 UTC using Firefox 1.5.0.4 on Windows XP:
I’ve always wished I could use Ruby for client side scripting! Please, please, please continue working on this!jman
Wrote at 2006-07-06 18:39 UTC using Firefox 1.5.0.4 on Windows XP:
That’s really great! I look forward to seeing it on RubyForge…nec
Wrote at 2006-07-06 21:08 UTC using Firefox 1.5.0.4 on Windows Server 2003:
can somebody tell me how is this going to be useful? You are still thinking in terms of javascript while still writing ruby code. I must be missnig something.Austin Taylor
Wrote at 2006-07-07 01:30 UTC using Safari 417.9.3 on Mac OS X:
So now all my javascript skills are deprecated. Great.But seriously, this could be huge. Your loops will be faster than using Prototype, you prevent bad practices, you make the code more concise… The downside is that it will be hard to think in javascript while writing ruby syntax. If we can think in ruby and get good output, we’re golden.
Daniel Lewis
Wrote at 2006-07-11 14:43 UTC using Firefox 1.0 on Windows XP:
Any chance of making a Javascript to Ruby Converter?(Or does anyone know of one?)
Daniel.