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 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.

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

···

On Jul 5, 2006, at 11:30 AM, Paul Battley 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!

[snip: example]

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.

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

Have fun!
Paul.

+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

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 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.

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.

···

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

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

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 05/07/06, Eric Hodel <drbrain@segment7.net> 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...

···

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

On 05/07/06, Eric Hodel <drbrain@segment7.net> wrote:

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com