### Install Dependencies and Setup ruby.wasm
Source: https://github.com/ruby/ruby.wasm/blob/main/CONTRIBUTING.md
Installs project dependencies, clones the repository recursively, sets up the environment, and compiles extension libraries. This is the initial setup step for contributing to the project.
```console
git clone https://github.com/ruby/ruby.wasm --recursive
cd ruby.wasm
./bin/setup
# Compile extension library
bundle exec rake compile
rake --tasks
```
--------------------------------
### Quick Start: Load Ruby in Browser/Node.js
Source: https://github.com/ruby/ruby.wasm/blob/main/packages/npm-packages/ruby-wasm-emscripten/README.md
A JavaScript example demonstrating how to load the Ruby interpreter using the loadRuby function from the @ruby/head-wasm-emscripten package in both browser and Node.js environments.
```javascript
import { loadRuby } from "@ruby/head-wasm-emscripten";
const main = async () => {
const args = ["--disable-gems", "-e", "puts 'Hello :)'"];
console.log(`$ ruby.wasm ${args.join(" ")}`);
const defaultModule = {
locateFile: (path) => "./node_modules/@ruby/head-wasm-emscripten/dist/" + path,
setStatus: (msg) => console.log(msg),
print: (line) => console.log(line),
arguments: args,
};
await loadRuby(defaultModule);
};
main();
```
--------------------------------
### Install @ruby/wasm-emscripten Package
Source: https://github.com/ruby/ruby.wasm/blob/main/packages/npm-packages/ruby-wasm-emscripten/README.md
Commands to install the @ruby/wasm-emscripten package and its specific versions, including snapshots and nightly builds, using npm.
```bash
# Install the latest stable version
$ npm install --save @ruby/wasm-emscripten@latest @ruby/head-wasm-emscripten@latest
# Install the nightly snapshot
$ npm install --save @ruby/head-wasm-emscripten@next
# Install a specific snapshot version
$ npm install --save @ruby/head-wasm-emscripten@2.7.1-2025-01-23-a
```
--------------------------------
### Install Release Channels for @ruby/wasm-wasi
Source: https://github.com/ruby/ruby.wasm/blob/main/CONTRIBUTING.md
Demonstrates how to install different release channels for the `@ruby/wasm-wasi` npm package. You can install the latest stable release, the pre-release version (`next`), or a specific snapshot version.
```console
$ npm install --save @ruby/wasm-wasi@latest
# or if you want the nightly snapshot
$ npm install --save @ruby/wasm-wasi@next
# or you can specify the exact snapshot version
$ npm install --save @ruby/wasm-wasi@2.7.1-2025-01-23-a
```
--------------------------------
### Install and Run Ruby.wasm in Node.js
Source: https://github.com/ruby/ruby.wasm/blob/main/docs/cheat_sheet.md
Installs the necessary packages and demonstrates how to instantiate a Ruby VM and execute Ruby code within a Node.js environment. Requires the `--experimental-wasi-unstable-preview1` flag for WASI support.
```console
npm install --save @ruby/3.4-wasm-wasi @ruby/wasm-wasi
```
```javascript
import fs from "fs/promises";
import { DefaultRubyVM } from "@ruby/wasm-wasi/dist/node";
const binary = await fs.readFile("./node_modules/@ruby/3.4-wasm-wasi/dist/ruby.wasm");
const module = await WebAssembly.compile(binary);
const { vm } = await DefaultRubyVM(module);
vm.eval(`puts "hello world"`);
```
```console
$ node --experimental-wasi-unstable-preview1 index.mjs
```
--------------------------------
### Build CRuby for Emscripten Target
Source: https://github.com/ruby/ruby.wasm/blob/main/CONTRIBUTING.md
Steps to build CRuby for WebAssembly/Emscripten. This requires installing Emscripten separately. The output of the build process will be in the `rubies` directory.
```console
# Build only a specific combination of ruby version, profile, and target
$ rake build:head-wasm32-unknown-emscripten-full
# Output is in the `rubies` directory
$ tree -L 3 rubies/head-wasm32-unknown-emscripten-full
```
--------------------------------
### Package Ruby App as WASI with rbwasm
Source: https://github.com/ruby/ruby.wasm/blob/main/README.md
Demonstrates packaging a Ruby application into a WASI-compatible WebAssembly module using the `rbwasm` tool. This involves installing the gem, downloading a prebuilt Ruby WASM runtime, organizing application files, and then using `rbwasm pack` to create the executable WASM file. The process requires `wasmtime` to run the packed application.
```bash
$ gem install ruby_wasm
# Download a prebuilt Ruby release
$ curl -LO https://github.com/ruby/ruby.wasm/releases/latest/download/ruby-3.4-wasm32-unknown-wasip1-full.tar.gz
$ tar xfz ruby-3.4-wasm32-unknown-wasip1-full.tar.gz
# Extract ruby binary not to pack itself
$ mv ruby-3.4-wasm32-unknown-wasip1-full/usr/local/bin/ruby ruby.wasm
# Put your app code
$ mkdir src
$ echo "puts 'Hello'" > src/my_app.rb
# Pack the whole directory under /usr and your app dir
$ rbwasm pack ruby.wasm --dir ./src::/src --dir ./ruby-3.4-wasm32-unknown-wasip1-full/usr::/usr -o my-ruby-app.wasm
# Run the packed scripts
$ wasmtime my-ruby-app.wasm /src/my_app.rb
```
--------------------------------
### Interact with JavaScript from Ruby: Call Methods
Source: https://github.com/ruby/ruby.wasm/blob/main/docs/cheat_sheet.md
Shows how to invoke JavaScript methods from Ruby code. It includes an example of calling `createElement` and demonstrates both direct method calls and symbol-based calls.
```ruby
require "js"
JS.global[:document].createElement("div")
JS.global[:document].call(:createElement, "div".to_js) # same as above
```
--------------------------------
### Re-bindgen from .wit files
Source: https://github.com/ruby/ruby.wasm/blob/main/CONTRIBUTING.md
Generates glue code from `.wit` files, which define WebAssembly module interfaces. This process requires the Rust compiler (`rustc`) and Cargo. The `wit-bindgen` tool is installed on demand. Ensure `cargo` is in your `PATH`.
```console
rake check:bindgen
```
--------------------------------
### Interact with JavaScript from Ruby: Instantiate Objects
Source: https://github.com/ruby/ruby.wasm/blob/main/docs/cheat_sheet.md
Demonstrates how to create new JavaScript instances from Ruby code using the `new` keyword via the `js` gem. The example shows creating a new `Date` object.
```ruby
require "js"
JS.global[:Date].new(2000, 9, 13)
```
--------------------------------
### Await JavaScript Promises in Ruby (Browser)
Source: https://github.com/ruby/ruby.wasm/blob/main/docs/cheat_sheet.md
Provides examples of how to `await` JavaScript Promises directly from Ruby code in a browser environment. This is achieved either by using the `data-eval="async"` attribute in `