TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Opal - Ruby to Javascript compiler

130 pointsby cap10morganover 12 years ago

13 comments

DanielRibeiroover 12 years ago
Cool idea. Not the first attempt though:<p><a href="http://hotruby.yukoba.jp/" rel="nofollow">http://hotruby.yukoba.jp/</a><p><a href="https://github.com/whitequark/coldruby" rel="nofollow">https://github.com/whitequark/coldruby</a><p><a href="http://rb2js.rubyforge.org/" rel="nofollow">http://rb2js.rubyforge.org/</a><p><a href="http://www.ntecs.de/blog/articles/2007/01/08/rubyjs-javascript-no-thank-you/" rel="nofollow">http://www.ntecs.de/blog/articles/2007/01/08/rubyjs-javascri...</a><p><a href="https://github.com/jessesielaff/red" rel="nofollow">https://github.com/jessesielaff/red</a><p><a href="http://www.playmycode.com/docs/quby" rel="nofollow">http://www.playmycode.com/docs/quby</a><p><a href="https://github.com/mattknox/8ball" rel="nofollow">https://github.com/mattknox/8ball</a><p>Unfortunately, none of them seem to be to handle the mor dynamic features of ruby, like <i>method_missing</i>, which diminishes the possibility of reusing existing ruby code in the browser.
评论 #4675109 未加载
评论 #4674716 未加载
评论 #4696960 未加载
dyscreteover 12 years ago
It's a great idea, but it seems ridiculous how even simple arithmetic gets translated to an unreadable mess of javascript.<p>puts 4 + 2 + 5 + (19 + 3 * 4) - 8 / 10<p>translates to:<p>(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))) })();
评论 #4674896 未加载
评论 #4675378 未加载
评论 #4676836 未加载
评论 #4679335 未加载
doktrinover 12 years ago
Being relatively new to cross-language compilation, how are one language's class APIs typically translated into another?<p>For example :<p><pre><code> [1,2,3].shuffle </code></pre> [turns into] =&#62;<p><pre><code> (function() { var __opal = Opal, self = __opal.top, __scope = __opal, nil = __opal.nil, __breaker = __opal.breaker, __slice = __opal.slice; return [1, 2, 3].$shuffle() })(); </code></pre> Of course, since JS arrays do not have a "shuffle" method, the output reads :<p><pre><code> TypeError: Object 1,2,3 has no method '$shuffle' </code></pre> Is the answer to simply use class/object methods that are present in both languages?
评论 #4677322 未加载
Xcelerateover 12 years ago
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: <a href="http://brendaneich.github.com/Strange-Loop-2012/#/21" rel="nofollow">http://brendaneich.github.com/Strange-Loop-2012/#/21</a>).<p>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 think this would be a good open source project idea: a tool to assist in the compilation of various languages to Javascript. Something to the effect of how source lines in C can be printed above their generated assembly. Anyone want to give it a shot?
评论 #4674761 未加载
评论 #4675227 未加载
jhrobertover 12 years ago
They lost me at the "nil" section.<p>RubyScript is what we need =&#62; efficient code, à la coffeescript, with a Ruby syntax.<p>There is some impedance mismatch between ruby &#38; javascript semantics. Trying to hide this fact has a cost in terms of performance.<p>I would love to have some ruby syntax as long as it does not compromize the speed of my code.<p>There is a trend these days where syntax and semantics are becoming orthogonal, that's nice.<p>Hence GoScript, PascalScript, PrologScript, CppScript, SmallScript...<p>Thanks to sourcemaps the issue of the generated code readability has disappeared. Only the performance matters from now on.
评论 #4677367 未加载
vjeuxover 12 years ago
Unfortunately their hash table uses Javascript objects. Therefore it does not behave exactly like Ruby.<p>Eg:<p><pre><code> # Opal h = Hash.new h['0'] = 1 h[0] = 2 print h # {"0"=&#62;2} # Ruby h = Hash.new h['0'] = 1 h[0] = 2 print h # {"0"=&#62;1, 0=&#62;2} </code></pre> It is very hard to reproduce those low level specifications without rebuilding everything from scratch sadly :(
评论 #4676839 未加载
1qaz2wsx3edcover 12 years ago
Yup, that's some good brainfuck:<p><pre><code> (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 function Foo() {}; Foo = __klass(__base, __super, "Foo", Foo); var Foo_prototype = Foo.prototype, __scope = Foo._scope; return Foo_prototype.$name = function() { if (this.name == null) this.name = nil; return this.name }, Foo_prototype['$name='] = function(val) { return this.name = val }, nil })(self, null); adam = __scope.Foo.$new(); adam['$name=']("Adam Beynon"); return self.$puts(adam.$name()); })();</code></pre>
评论 #4674621 未加载
jonny_ehover 12 years ago
While very impressive it makes me nervous. The JS it outputs just in the basic tutorial looks extremely complex, I'd hate to debug that code! Compare with CoffeeScript, where the output is quite easy to follow, and therefore debug.
评论 #4675198 未加载
评论 #4676831 未加载
lexy0202over 12 years ago
Can you interact with the dom inside opal?
评论 #4675003 未加载
sambunaover 12 years ago
I certainly see future for this!
jblockover 12 years ago
Source maps? Source maps.
5vforestover 12 years ago
What the use case for something like this? Am I missing something?
评论 #4674715 未加载
评论 #4675035 未加载
评论 #4674665 未加载
评论 #4674618 未加载
goggles99over 12 years ago
Can't those standards committees just do something right for once and create a standard for a vm that will run in the browser (a la Dartium)? Stop this monopoly and oppression of innovation!!!