Skip to content

Ruby::Box support—is it possible in Ruby WASM? #645

Description

@brandonzylstra

Is it possible to enable Ruby::Box in Ruby WASM?

Activity

ledsun commented on May 1, 2026

@ledsun
Contributor

Yes, Ruby::Box can run on ruby.wasm.

Importantly, to enable Ruby::Box, you must specify RUBY_BOX=1 as an environment variable when starting the Ruby VM. Setting ENV[“RUBY_BOX”] = “1” from within Ruby code is too late.

For example, this works on Node.js:

import fs from "fs/promises";
import { DefaultRubyVM } from "@ruby/wasm-wasi/dist/node";

const binary = await fs.readFile(
  "./node_modules/@ruby/head-wasm-wasi/dist/ruby.wasm",
);
const module = await WebAssembly.compile(binary);

const { vm } = await DefaultRubyVM(module, {
  env: {
    RUBY_BOX: "1",
  },
});

const result = vm.eval(`
  box = Ruby::Box.new
  box.eval <<~RUBY
    X = 123
  RUBY

  puts "Ruby::Box.enabled?: #{Ruby::Box.enabled?}"
  puts "box::X: #{box::X}"
`);

Example output:

$ node ruby-box.node.js
(node:8080) ExperimentalWarning: WASI is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
ruby.wasm: warning: Ruby::Box is experimental, and the behavior may change in the future!
See https://docs.ruby-lang.org/en/master/Ruby/Box.html for known issues, etc.
Ruby::Box.enabled?: true
box::X: 123

The same applies in browsers if you instantiate the VM manually, e.g. by passing env to DefaultRubyVM:

const { vm } = await DefaultRubyVM(module, {
  env: {
    RUBY_BOX: "1",
  },
});

Note

Currently, browser.script.iife.js does not provide a way to pass environment variables from HTML.

brandonzylstra commented on May 2, 2026

@brandonzylstra
ContributorAuthor

Are there any plans to update browser.script.iffe.js to make it easy to pass in custom options?

ledsun commented on Aug 16, 2026

@ledsun
Contributor

This is now supported as of ruby.wasm v2.10.0, via #649.

browser.script.iife.js accepts a data-env attribute containing a JSON object, so Ruby::Box can now be enabled directly from HTML:

<script
  src="https://cdn.jsdelivr.net/npm/@ruby/4.0-wasm-wasi@2.10.1/dist/browser.script.iife.js"
  data-env='{"RUBY_BOX":"1"}'
></script>

<script type="text/ruby">
  box = Ruby::Box.new
  box.eval("X = 123")

  puts "Ruby::Box.enabled?: #{Ruby::Box.enabled?}"
  puts "box::X: #{box::X}"
</script>

The values in data-env must be strings.

I believe this addresses your original question and the subsequent requests.
Please let me know if there’s anything else you need. Otherwise, I think it’s fine to close this issue.

brandonzylstra commented on Aug 17, 2026

@brandonzylstra
ContributorAuthor

Thanks I'll plan to try that and report back. (I previously just altered the JS file, which has worked out well.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions