Opal is a ruby to javascript compiler. It is source-to-source, making it fast as a runtime. Opal includes a compiler (which can be run in any browser), a corelib and runtime implementation. The corelib/runtime is also very small (10.8kb gzipped).
Opal is hosted on github,
has a Freenode IRC channel at #opal, and on twitter
@opalrb.
Opal compiles ruby ahead of time into javascript to run on the client. Opal supports blocks, procs, classes, modules and more!
Opal fully supports `method_missing` on all objects and classes to allow full metaprogramming on the client.
`Native` is a class provided to wrap native objects so that their properties and methods can be called directly from ruby code.
X-Strings in opal are used to write javascript within ruby code, making it easier to call and wrap javascript code within your code.
`opal-parser.js` allows you to compile and run ruby code directly from script tags or strings for runtime `eval()` support.
Opal compiles into clean, readable code to help with debugging. Variables, ivars and method names are preserved in the output.
[1, 2, 3, 4].each do |a|
puts a
end
class Foo
attr_accessor :name
def method_missing(sym, *args, &block)
puts "You tried to call: #{sym}"
end
end
adam = Foo.new
adam.name = 'Adam Beynon'
puts adam.name
adam.do_task
# Output (in your browser console):
#
# 1
# 2
# 3
# 4
# Adam Beynon
# You tried to call: do_task
Try this code in your browser!
Install Opal from RubyGems:
$ gem install opal
Or include it in your Gemfile for Bundler:
gem 'opal'