Project

opalracer

0.0
No commit activity in last 3 years
No release in over 3 years
An isolated Ruby runtime that lets you execute Ruby scripts in isolation from the main Ruby interpreter
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

OpalRacer

Usage

Create a new execution context and insert some ruby:

# test.rb
ctx = OpalRacer.new
ctx.eval_ruby('class Foo; end; puts "HELL YES"')
puts Foo

Now run the script:

$ ruby test.rb
HELL YES
test.rb:4:in `<main>': uninitialized constant Foo (NameError)

How it Works

There's no subprocess tomfoolery going on here. And since typical Ruby runtimes permit only one execution context, and since I've defined a global constant 'Foo', it must be defined globally. But it isn't.

That's because OpalRuby uses Opal to transpile your Ruby into JavaScript. Isolating and executing JavaScript code within the the Ruby interpreter is easy thanks to therubyracer and V8.

Check out Opal and be amazed at how well it does at running Ruby.

Buy WHY??!

A fair question.

When you're a responsible human, you can create code that plays well with the rest of the world. You can avoid defining lots of globals. You can avoid reaching into external objects and altering them. And you can carefully manage your own dependencies. Ruby can give you no guarantees that your code will not be affected by a rogue gem or library. Sometimes you really just need some isolation.

OpalRacer is admittedly a silly hack, but it does allow you to make certain guarantees about how various worlds of Ruby can interact.