This document discusses using Ruby code in the browser through a Ruby to JavaScript compiler called Opal. Opal allows you to write Ruby code and compile it to JavaScript, enabling the use of Ruby concepts and syntax directly in web applications. Some key points covered include how Opal maps Ruby concepts like self, nil, and truthy values to their JavaScript equivalents, a demo of a calculator application built with Opal, and answers to common questions about whether and how Ruby and JavaScript can interact when using Opal. Benchmarks are also mentioned comparing performance of Ruby code compiled to JS versus interpreting Ruby directly in the browser.
19. Opal
you can precompile ruby code to js
generated code is JavaScript
concepts are mapped directly to
underlying JavaScript features and
objects when possible
20. Opal for the
Rubyist
self is compiled to this (and the
runtime properly binds this to the
correct self value)
nil is a JavaScript var (because in
Ruby, nil is an actual object)
21. Opal for the
Rubyist
"" 0 and [] is truthy
true and false are of the Boolean
class, not TrueClass and FalseClass
strings are immutable (because js
strings are immutable)
23. FAQA
can you use js inside ruby? yes
can you use ruby inside js? yes
can you access the browser elements
(like the location bar, control the
window, etc)? yes
can I use jQuery? yes
24. FAQA
is it really ruby? yes (5,122 rubyspec
since 10 June 2015)
so that means you have
method_missing? yes
can I just write ruby without
precompiling it? yes, but it's less
performant