Convert Ruby to JavaScript

This is old! You should also check out RubyJS for a fuller implementation of the same idea.

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.

Comments

Skip to the comment form

  1. Florian Groß

    Wrote at 2006-07-05 23:02 UTC using Firefox 1.5.0.4 on Windows XP:

    Woah, how very cool!
  2. 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!
  3. _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.
  4. 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.
  5. 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? :)
  6. Folkestone Gerald

    Wrote at 2006-07-06 10:09 UTC using Firefox 1.5.0.1 on Windows XP:

    Interesting idea, cheers…
  7. 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 btw
  8. Ben

    Wrote at 2006-07-06 11:12 UTC using Firefox 1.5.0.4 on Windows XP:

    Argh that’s terrible!
  9. 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…
  10. 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.
  11. 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.
  12. 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!
  13. 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…
  14. nec

    Wrote at 2006-07-06 21:08 UTC using Firefox 1.5.0.4 on Windows XP 64 or 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.
  15. 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.
  16. 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.
  17. Jason

    Wrote at 2008-09-20 02:43 UTC using Firefox 3.0.1 on Windows XP:

    what is the status on the Ruby2JS project?
  18. Paul Battley

    Wrote at 2008-09-20 13:00 UTC using Firefox 3.0.1 on Linux:

    Jason, it’s moribund, and surpassed by rubyjs.
  19. Danilo

    Wrote at 2009-03-03 17:55 UTC using Firefox 3.0.6 on Windows XP:

    Thanks for this help. I will try it. What I need is to convert an API (Facebook chat) to javascript. Thanks again
  20. Douglas

    Wrote at 2009-09-25 18:46 UTC using Safari 531.9 on Mac OS X:

    Just a quick point: the self = this; lines are setting a global variable “self”, instead of a local variable. You’ll need to use var self = this; if you want to use local ones.
  21. Tom Robinson

    Wrote at 2009-09-25 19:30 UTC using Safari 531.1 on Mac OS X:

    I did something like this (also only a proof of concept) that uses the Objective-J runtime (which is very similar to Ruby’s runtime) on JavaScript:

    http://github.com/tlrobinson/cappruby/tree/master
  22. Manoj

    Wrote at 2009-10-11 11:05 UTC using Firefox 3.5.3 on Windows Vista:

    hi,
    i dont know this is right message … can you tell a way to convert a java applet in to ruby??

    I really need this.. help

Leave a comment

Please read the comment guidelines before posting. Comments are Gravatar-enabled. Your email address will not be published.

To prove that you’re human, type human in the Bot check field.

Trying to post some program output or a long code sample? Please use a paste service and link to it instead.