There's opal-irb
and opal-jquery
and vienna
but is there any way to use gems directly in the browser via Opal?
2 Answers
You can add a gem's lib
path to Opal load paths by using Opal.use_gem
Common pitfalls are:
- use of String mutability
- relying on the difference between String and Symbol
- shelling out (
``
and%x{}
)
Available tools to fix/workaround some of those issues are:
- stubbing files
Opal::Processor.stub_file('fileutils')
- hiding code branches at compile time using
RUBY_ENGINE
, example:unless RUBY_ENGINE == 'opal' unparsable/breaking code here end
You can look at the opal-rspec
source code to see this stuff in action:

- 989
- 7
- 18
-
Hi there Elia, I'm trying to use the "faker" gem with Opal. Could you please explain with a bit more detail where should I put the "Opal.use_gem('faker')" line ? I've put it at the top of Rakefile, after "require 'opal'" but then I get ```Sprockets::FileNotFound: couldn't find file 'psych'``` when I run rake build..? Thank you – Redoman Oct 02 '14 at 09:27
-
If you're using rails you should put it into an initializer – Elia Schito Oct 03 '14 at 08:25
-
I am not using Rails? – Redoman Oct 03 '14 at 11:37
-
I guess then it depends on how you organize your project. Anyways the code should be executed during the setup, e.g. just after requiring opal MRI/CRuby side. – Elia Schito Oct 05 '14 at 13:39
-
My project structure is exactly the same of the basic static application example at http://opalrb.org/docs/static_applications/ and I use "Opal.use_gem('faker')" right after the require 'opal' line. Any clues? – Redoman Oct 05 '14 at 18:29
-
I'd put that just after `env.append_path "app"` in the Rakefile (from http://opalrb.org/docs/static_applications/) – Elia Schito Oct 08 '14 at 17:32
-
`builder = Opal::Builder.new; builder.path_reader.append_paths("path/to/the/gem_name/lib"); builder.build_str('require "opal"; require "gem_name"', '(inline)')` – Redoman Apr 27 '17 at 06:33
-
also, from command line: `opal --include "path/to/gem/lib" --require "gem_name" --compile source.rb > app.js` – Redoman Apr 27 '17 at 06:48
As usual the answer is yes and no at the same time, depending on your point of view. Opal will turn your ruby (all of it) into JavaScript and provides an appropriate run time. If you require a gem it will be required during the compilation process and will be included into the generated JavaScript. You may freely use the generated ruby classes in your ruby code (which again ends up being compiled into JavaScript).
So you can require gems, but bear in mind that their requires will also be required, so will end up with a nightmare of a JavaScript file if you are not careful. Technically you are still not running ruby in the browser, it all had to be compiled to JavaScript for that purpose. However you can run the code generated from your ruby and the required gems, though it will have become JavaScript during the process (and you will have to debug it as such). There are some limitations to this approach though, you will have to bear in mind JavaScript Number
and String
properties (aka only immutable String
s), but within these limits you may share your code between the server and the client.

- 4,481
- 2
- 32
- 42
-
-
2No way to have those in your browser. An alternative is to wrap NodeJS libs and replicate the gem api if you migrate some code to node. – Elia Schito Jul 05 '14 at 15:25