Opal is a Ruby to JavaScript source-to-source compiler.
It comes packed with the Ruby corelib you know and love.
It is both fast as a runtime and small in its footprint.
Opal is truly amazing and it has taken me in a whole new direction.โ @mistergibson from Gitter, on Jan 22 2018
Opal 0.11 is a blessing! Its a giant step forward to ruby on the client! Its just amazing what bugs i find in existing 0.10 code and do keep wondering, "How did this ever work at all?" I am amzed, fantastic ๐๐๐๐โ @janbiedermann from Gitter, on Jan 20 2018
These guys are great. We have a large production app (www.catprint.com) that thanks to opal is 100% RUBY. The UI part is about 17K of opal code, using the hyperstack (www.hyperstack.org) framework.โ @catmando from Gitter, on Dec 1 2018
In practice Opal == MRI Ruby, very reliable and solid.
class User
attr_accessor :name
def initialize(name)
@name = name
end
def admin?
@name == 'Admin'
end
end
user = User.new('Bob')
# the output will go to your browser's console
puts user
puts user.admin?
Add rack to your Gemfile.
gem 'rack'
Setup the Opal rack-app in your config.ru as follows:
require 'opal'
run(Opal::SimpleServer.new do |s|
# the name of the ruby file to load. To use more files they must be required from here (see app)
s.main = 'application'
# the directory where the code is (add to opal load path )
s.append_path 'app'
# need to set the index explicitly for opal server to pick it up
s.index_path = 'index.html.erb'
end)
Add a file named hello_world.js.rb to app/ with your hello world:
require 'opal'
require 'native'
$$.alert 'Hello World from Opal!'
Then start the server with bundle exec rackup and visit http://localhost:9292.
Learn more about Opal Sprockets
Add opal-rails to your Rails app's Gemfile:
gem 'opal-rails'
Rename app/assets/javascripts/application.js to app/assets/javascripts/application.js.rb and replace its contents with this code:
require 'opal'
require 'opal_ujs'
require 'turbolinks'
require_tree '.'
Create a "Hello World" controller:
bin/rails generate controller HelloWorld index
Replace the contents of app/assets/javascripts/hello_world.js.rb with:
Document.ready? do
Element.find('body').html = '<h1>Hello World from Opal!</h1>'
end
Start the server with bin/rails server and visit: http://localhost:3000/hello_world/index.
Install opal from Rubygems:
gem install opal
Write this code to hello_world.js.rb:
require 'ostruct'
greeting = OpenStruct.new(type: :Hello, target: :World, source: :Opal)
puts "#{greeting.type} #{greeting.target} from #{greeting.source}!"
Run it with Node.js (assuming it's installed):
opal hello_world.js.rb
# => Hello World from Opal!
Compile it to a JavaScript file:
opal -c hello_world.js.rb > hello_world.js
node hello_world.js
# => Hello World from Opal!
For a full list of supported options see:
opal --help
Opal is hosted on GitHub .
You can join the community by chatting on Gitter at opal/opal or on Freenode IRC (channel: #opal).
Ask questions on stackoverflow by using the #opalrb tag.
Discuss on the mailing list or on Twitter @opalrb.