Live data from Hacker News

Opal - Ruby to Javascript compiler

opalrb.org

11–20 of 54 posts

Re: Opal - Ruby to Javascript compiler

#11
post #4

What the use case for something like this? Am I missing something?

Using the language you're comfortable with client-side. Though what this really shows, once again, is that we really need a good, open, virtual machine specification for client-side byte code so we aren't stuck with the same javascript hammer for all problems.

Re: Opal - Ruby to Javascript compiler

#12

Cool idea. Not the first attempt though: http://hotruby.yukoba.jp/ https://github.com/whitequark/coldruby http://rb2js.rubyforge.org/ http://www.ntecs.de/blog/articles/2007/01/08/rubyjs-javascri... https://github.com/jessesielaff/red http://www.playmycode.com/docs/quby https://github.com/mattknox/8ball Unfortunately, none of them seem to be to handle the mor dynamic features of ruby, like method_missing , which dimin…

Javascript Harmony (ES6) Proxies will allow to define getter catchall and other kinds of traps.

See https://developer.mozilla.org/en-US/docs/JavaScript/Referenc...

Re: Opal - Ruby to Javascript compiler

#13

I really like this idea of compiling code to Javascript. Brenden Eich makes some good points for it in his series of slides (starting here: http://brendaneich.github.com/Strange-Loop-2012/#/21 ). I'm working on a little programming language myself that compiles to Javascript. In most languages that do this, I wish there was better information on the mapping between the domain language and its Javascript codomain. I t…

I think what you've described would be mostly accomplished by source maps: http://www.html5rocks.com/en/tutorials/developertools/source...

Re: Opal - Ruby to Javascript compiler

#14
post #6

Yup, that's some good brainfuck: (function() { var __opal = Opal, self = __opal.top, __scope = __opal, nil = __opal.nil, __breaker = __opal.breaker, __slice = __opal.slice, __klass = __opal.klass; var adam = nil, __a, __b; (__b = [1, 2, 3, 4], __b.$each._p = (__a = function(a) { if (a == null) a = nil; return this.$puts(a) }, __a._s = self, __a), __b.$each()); (function(__base, __super){ // line 5, (file), class Foo…

If you look at this rationally, it's actually pretty sane. It's just very simple JavaScript OOP inline with translated code. Even more beautiful is the output of modules: module Run def run puts "I am running!" end end module Jump def jump puts "I am jumping!" end end class Foo include Run include Jump end mario = Foo.new mario.run mario.jump translates to: (function() { var __opal = Opal, self = __opal.top, __scope…

While we are posting contrived examples. What was ever wrong with this?

    var Foo = function() {};

    Foo.prototype.jump = function() {
        return "I jump."
    };

    Foo.prototype.run = function() {
        return "I run."
    };

    var mario = new Foo();
    mario.run();
    mario.jump();
I can't imagine that opal code running nearly as fast as javascript. And if we are waving around compiled js - coffeescript does a much better job of being readable. You just black-boxed the whole thing. I like to know what is happening with my js.

Cool for the rubyist in you i guess. I just kinda go o_0

Re: Opal - Ruby to Javascript compiler

#15
post #12

Cool idea. Not the first attempt though: http://hotruby.yukoba.jp/ https://github.com/whitequark/coldruby http://rb2js.rubyforge.org/ http://www.ntecs.de/blog/articles/2007/01/08/rubyjs-javascri... https://github.com/jessesielaff/red http://www.playmycode.com/docs/quby https://github.com/mattknox/8ball Unfortunately, none of them seem to be to handle the mor dynamic features of ruby, like method_missing , which dimin…

Javascript Harmony (ES6) Proxies will allow to define getter catchall and other kinds of traps. See https://developer.mozilla.org/en-US/docs/JavaScript/Referenc...

Minor FYI: for this who want to play with Proxies right now, you can use a recent-ish copy of node invoked with node --harmony_proxies Enjoy!

Re: Opal - Ruby to Javascript compiler

#16
It's a great idea, but it seems ridiculous how even simple arithmetic gets translated to an unreadable mess of javascript.

puts 4 + 2 + 5 + (19 + 3 * 4) - 8 / 10

translates to:

(function() { var __opal = Opal, self = __opal.top, __scope = __opal, nil = __opal.nil, __breaker = __opal.breaker, __slice = __opal.slice; var __a, __b, __c, __d, __e, __f, __g, __h; return self.$puts((__a = (__c = (__e = (__g = 4, __h = 2, typeof(__g) === 'number' ? __g + __h : __g['$+'](__h)), __f = 5, typeof(__e) === 'number' ? __e + __f : __e['$+'](__f)), __d = (__e = 19, __f = (__g = 3, __h = 4, typeof(__g) === 'number' ? __g * __h : __g['$*'](__h)), typeof(__e) === 'number' ? __e + __f : __e['$+'](__f)), typeof(__c) === 'number' ? __c + __d : __c['$+'](__d)), __b = (__c = 8, __d = 10, typeof(__c) === 'number' ? __c / __d : __c['$/'](__d)), typeof(__a) === 'number' ? __a - __b : __a['$-'](__b))) })();

Re: Opal - Ruby to Javascript compiler

#18

It's a great idea, but it seems ridiculous how even simple arithmetic gets translated to an unreadable mess of javascript. puts 4 + 2 + 5 + (19 + 3 * 4) - 8 / 10 translates to: (function() { var __opal = Opal, self = __opal.top, __scope = __opal, nil = __opal.nil, __breaker = __opal.breaker, __slice = __opal.slice; var __a, __b, __c, __d, __e, __f, __g, __h; return self.$puts((__a = (__c = (__e = (__g = 4, __h = 2, t…

It's really not terribly complicated to figure out what it does, here's the formatted version for adding two numbers

    (function() {
          var __opal = Opal, self = __opal.top, __scope = __opal, nil = __opal.nil, __breaker = __opal.breaker, __slice = __opal.slice;
          var __a, __b;
          return self.$puts((__a = 1, __b = 2, typeof(__a) === 'number' ? __a + __b : __a['$+'](__b)))
    })();
The first line just declares some Opal-specific variables (__scope, nil, __breaker, etc.), which I guess are created whether or not they're actually used. The second line defines the temporary variables used to store the numbers. Then, depending on the type, it either actually adds them or use its own '$+' function, which I guess it adds to the prototype for all objects.

In terms of having to ever debug something like that, I agree that it'd probably be a huge pain.

Post reply on HN