Skip to content

Getting Started with CRuby

Yuta Saito edited this page Jun 5, 2022 · 5 revisions

wasi-vfs provides a language and host-agnostic virtual filesystem layer for WASI.

This page describes how to utilize it with Ruby language.

Example: Pack standard library and your application

The prebuilt Ruby release is distributed in ruby.wasm and it links libwasi_vfs.a, so you can pack files without re-building the Ruby.

# Download a prebuilt Ruby release
$ curl -LO https://github.com/ruby/ruby.wasm/releases/download/2022-03-28-a/ruby-head-wasm32-unknown-wasi-full.tar.gz
$ tar xfz ruby-head-wasm32-unknown-wasi-full.tar.gz

# Extract ruby binary not to pack itself
$ mv head-wasm32-unknown-wasi-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
$ wasi-vfs pack ruby.wasm --mapdir /src::./src --mapdir /usr::./head-wasm32-unknown-wasi-full/usr -o my-ruby-app.wasm

# Run the packed scripts
$ wasmtime my-ruby-app.wasm -- /src/my_app.rb
Hello

Tips: Omit arguments at launching the application

You can preset the script file argument by wasi-preset-args

$ wasi-preset-args my-ruby-app.wasm -o my-ruby-app.wasm -- /src/my_app.rb
$ wasmtime my-ruby-app.wasm
Hello