Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use Ruby gems in Opal?

Tags:

ruby

gem

opalrb

There's opal-irb and opal-jquery and vienna but is there any way to use gems directly in the browser via Opal?

like image 727
themirror Avatar asked Jul 04 '14 11:07

themirror


People also ask

What stones can be worn with Opal?

Opal can be worn as finger rings/pendants made of silver, platinum, gold/white gold or Panchdhatu.

Can I wear Ruby with Opal?

The planet of Venus clashes with the following celestial bodies: The Sun, The Moon, and Jupiter. This is why the Opal Gemstone should not be worn with Ruby, Pearl, or Topaz gemstones.

What can Ruby gem be used for?

What are rubies used for? The most common use for rubies is jewelry—stunning jewelry! However, both natural and synthetic rubies are used in a variety of applications—such as watchmaking, medical instruments, and lasers—because of their incredible strength and red fluorescence.

Can opal be worn as a chain?

While opals are very pretty, they're soft and susceptible to scratches. Try wearing them as a necklace or bracelet rather than as a ring to expose them to fewer dings and scrapes.


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:

https://github.com/opal/opal-rspec

like image 174
Elia Schito Avatar answered Oct 21 '22 03:10

Elia Schito


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 Strings), but within these limits you may share your code between the server and the client.

like image 37
Patru Avatar answered Oct 21 '22 03:10

Patru