Opal does not aim to be 100% comaptible with other ruby implementations, but does so where the generated code can be efficient on all modern web browsers - including older versions of IE and mobile devices.
Install the gem:
$ gem install opal
opal command should then be available. To run the simple repl use:
The quickest way to get opal running is to use the project generator. Simply run the command:
opal init my_project
replacing "my_project" with any name. This will make a "my_project" directory with a Rakefile, html document and libs needed for running opal in the browser.
Using opal in the browser
Opal runs directly in the browser, and is distributed as two files,
opal-parser.js. To just run precompiled code, just the
opal.js runtime is required which includes the runtime and opals
implementation of the ruby core library (pre compiled).
To evaluate ruby code directly in the browser,
opal-parser.js is also
required which will also load any ruby code found in script tags.
The Rakefile has a task to build your ruby project, so just run:
index.html in a browser, and now it should run. Edit, build and
run to suit.
This repo contains the code for the opal gem as well as the opal core
library and runtime. Files inside
lib/ are the files that
are used as part of the gem and run directly on your ruby environment.
corelib/ contains opal's core library implementation and is not used
directly by the gem. These files are precompiled during development
ready to be used in the gem or in a browser.
directly by the gem, but is built ready to use in the js contexts that
stdlib/ contains the stdlib files that opal comes packaged with. The
gem does use these, but only as required. Opal does not include the full
optimal performance. These can be
require() at runtime.
opal-parser.js are included in the gem, but not the
source repo. They are the latest built versions of opal and its parser
which are built before the gem is published.
Differences from ruby
To optimize method dispatch,
method_missing is, by default, turned off
in opal. It can easily be enabled by passing
:method_missing => true
in the parser options.
Immutable strings and removed symbols
All strings in opal are immutable to make them easier to map onto
string without a wrapper so that they can be passed back between code
bases easily. Also, symbol syntax is maintained, but all symbols just
Symbol class is also therefore
Unified Numeric class
All numbers in opal are members of the
Numeric class. The
Float classes are removed.
Unified Boolean class
counterparts which means that they both become instances of the
Boolean class and opal removes the