Re:Opal: Ruby in Your Browser – the Basics
by Skoofoo on Saturday April 05, 2014 @10:47AM
(#7537207)
You have to drop down to JavaScript at some point, though in practice you would not write this much JavaScript mixed with Ruby in an app. The joy of using Opal is having the JavaScript wrapped away in a module, letting you use pure Ruby for your app.
The author used a lot of JavaScript here, likely for teaching purposes, but he could instead have used pure Ruby to interface with canvas using opal-browser. https://github.com/opal/opal-browser/blob/master/opal/browse...
Yea. They have a canvas wrapper that this guy did not use so you don't have to do that. With that being said, this project https://github.com/rubys/ruby2js is more appealing to me. Sure it doesn't pass more RubySpec tests than Rubinius like Opal does, but it works.
The Angular interoperability is especially appealing to me. It also generates readable javascript.
Re:Opal: Ruby in Your Browser – the Basics
by ylluminate on Saturday April 05, 2014 @04:02PM
(#7538245)
Use opal-browser (https://github.com/opal/opal-browser). Great stuff.
Re:Opal: Ruby in Your Browser – the Basics
by adambeynon on Saturday April 05, 2014 @10:54AM
(#7537226)
The example in this post is definitely a bad example of inline js in Opal. This is better handled by the `Native` bridge class, or writing a wrapper for it.
On the other hand, being able to drop down to native javascript anytime you like is a huge win.
Re:Opal: Ruby in Your Browser – the Basics
by ylluminate on Saturday April 05, 2014 @04:01PM
(#7538238)
@adam maybe you can give us a tutorial on doing this properly? Opal is amazing and being able to wrap things like ImpactJS and other js libs is enormously powerful.
Re:Opal: Ruby in Your Browser – the Basics
by isbadawi on Saturday April 05, 2014 @06:16AM
(#7536610)
Could you explain what is so offensive about using `...` here?
Re:Opal: Ruby in Your Browser – the Basics
by bshimmin on Saturday April 05, 2014 @06:51AM
(#7536651)
In 48 lines of code, he has 12 lines where he's evaluating JavaScript between backticks (and not all those 48 lines are significant). If you're having to do that much evaluation of raw JavaScript, then what is the point of using a compile-to-JS language at all?
With CoffeeScript, I think I've "had" to use backticks all of about twice (I probably didn't actually have to, but it seemed easier at the time - I don't remember the exact circumstances).
Also, as others have said, the JavaScript that this outputs is a real mess (in comparison with, say, CoffeeScript).
I wouldn't mind the outputed javascript - it's unfair to compare it with coffeescript as cs is just a thin syntax layer whereas opal giver you all the goodies of rubyan oop (like, method_missing? :) ), so there is a price that needs to be paid for this. But having to drop to raw js everywhere kind of nulls the advantage. Just think of all the errors that a linter or parser could have caught, but now it can't, and on top of this add it to the inherent pain of debugging a transcompiled language and... not fun :| I liked the idea of ruby to js but this doesn't seem like an acceptable solution.
`...` should be a last resort option for when you just need to do something that can't be done any other way, not something that should be peppered over the entire code.