# Automatic Ruby to JavaScript conversion

**URL:** <https://www.ruby-forum.com/t/automatic-ruby-to-javascript-conversion/97209>\
**Category:** Ruby\
**Created:** [July 5, 2006, 5:33pm UTC](https://www.ruby-forum.com/t/automatic-ruby-to-javascript-conversion/97209 "2006-07-05T17:33:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [July 5, 2006, 5:33pm UTC](https://www.ruby-forum.com/t/automatic-ruby-to-javascript-conversion/97209/1 "2006-07-05T17:33:28Z")

</div>

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](http://po-ru.com/files/ruby2js.tar.gz)

Have fun!  
Paul.

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [July 5, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/automatic-ruby-to-javascript-conversion/97209/2 "2006-07-05T18:21:49Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [July 5, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/automatic-ruby-to-javascript-conversion/97209/3 "2006-07-05T20:23:27Z")

</div>

+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

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [July 5, 2006, 9:10pm UTC](https://www.ruby-forum.com/t/automatic-ruby-to-javascript-conversion/97209/4 "2006-07-05T21:10:26Z")

</div>

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

- Jake McArthur

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [July 5, 2006, 10:36pm UTC](https://www.ruby-forum.com/t/automatic-ruby-to-javascript-conversion/97209/5 "2006-07-05T22:36:54Z")

</div>

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. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [July 5, 2006, 10:58pm UTC](https://www.ruby-forum.com/t/automatic-ruby-to-javascript-conversion/97209/6 "2006-07-05T22:58:27Z")

</div>

On 05/07/06, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 🙂 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.

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [July 5, 2006, 11:16pm UTC](https://www.ruby-forum.com/t/automatic-ruby-to-javascript-conversion/97209/7 "2006-07-05T23:16:57Z")

</div>

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

> On 05/07/06, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 🙂 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. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)
