More progress on rb2js

千里之行,始於足下 ―― 道德經

After some productive coding this morning, I’ve now got a basic Ruby object system working in JavaScript. I’ve had a lot of help from the Ruby Hacking Guide (written in Japanese and partially translated into English) regarding how objects are implemented inside Ruby.

I’ve also implemented some basic String and Kernel methods so that the following JavaScript code actually works:

var s ='Hello');
rbSendMessage(s, '<<', [' World']);
rbSendMessage(RbRootObject.getConstant('Kernel'), 'puts', [s]);

Ugly, isn’t it? Just remember that the goal is not to produce idiomatic JavaScript code, but rather something that works like Ruby and that can be targeted by the rb2js compiler. It’s analogous to this in Ruby:

s = 'Hello'
s << ' World'

Although it looks simple, it’s actually a really big step towards emulating Ruby’s behaviour in JavaScript. We can have mutable strings, for example, even though the underlying platform (JS) doesn’t support them natively.

I haven’t implemented metaclasses yet, so instantiation isn’t yet possible except in the special case of boxed strings. Still, getting that ‘Hello World’ printed makes me feel that I’ve got over the first hill on the journey.


  1. Dr Nic

    Wrote at 2006-07-20 09:26 UTC using Firefox on Windows XP:

    Have you uploaded the code to rubyforge yet?
  2. Paul Battley

    Wrote at 2006-07-20 09:53 UTC using Firefox on Mac OS X:

    Not yet. I’ll try to do it tonight, and drop you an email when it’s up.
  3. Paul Battley

    Wrote at 2006-07-20 22:29 UTC using Firefox on Mac OS X:

    OK, the code is up. See the project home page or just grab it with Subversion:
    svn co svn://

    I haven’t uploaded the original conversion code, as it’s already available online, and needs a total rewrite to work with the new model.
  4. Xiangdian

    Wrote at 2006-07-23 12:30 UTC using Internet Explorer 6.0 on Windows XP:

    Hey my old friend, I appreciate very much your Chinese quotation at the beginning, since I started to read this great ancient book seriously in 1995, it has been impressed me deeply. Hope see you again in China one day. Cheers!