File: lang-ruby.md

package info (click to toggle)
rust-wasmtime 26.0.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 48,504 kB
  • sloc: ansic: 4,003; sh: 561; javascript: 542; cpp: 254; asm: 175; ml: 96; makefile: 55
file content (63 lines) | stat: -rw-r--r-- 1,815 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Using WebAssembly from Ruby

Wasmtime [is available on RubyGems](https://rubygems.org/gems/wasmtime) and can
be used programmatically to interact with Wasm modules. This guide will go over
installing the Wasmtime gem and running a simple Wasm module from Ruby.

Make sure you've got Ruby 3.0 or newer installed locally, and we can get
started!

## Getting started and simple example

First, copy this example WebAssembly text module into your project. It exports
a function for calculating the greatest common denominator of two numbers.

```wat
{{#include ../examples/gcd.wat}}
```

Next, install the Wasmtime Ruby gems by either adding it your project's
`Gemfile`:

```bash
bundle add wasmtime
```

Or by using the `gem` command directly:

```bash
gem install wasmtime
```

The gem has a Rust-based native extension, but thanks to precompiled gems, you
should not have to compile anything. It'll just work!

Now that you have the Wasmtime gem installed, let's create a Ruby script to
execute the `gcd` module from before.

```ruby
require "wasmtime"

engine = Wasmtime::Engine.new
mod = Wasmtime::Module.from_file(engine, "gcd.wat")
store = Wasmtime::Store.new(engine)
instance = Wasmtime::Instance.new(store, mod)

puts "gcd(27, 6) = #{instance.invoke("gcd", 27, 6)}"
```

This script should output

```bash
gcd(27, 6) = 3
```

If this is the output you see, congrats! You've successfully ran your first
WebAssembly code in Ruby!

## More examples and contributing

To learn more, check out the [more advanced examples](https://github.com/bytecodealliance/wasmtime-rb/tree/main/examples)
and the [API documentation](https://bytecodealliance.github.io/wasmtime-rb/latest/).
If you have any questions, do not hesitate to open an issue on the
[GitHub repository](https://github.com/bytecodealliance/wasmtime-rb).