Spinel: Ruby, compiled
Every cell so far ran on CRuby, the Ruby Matz started in 1993. CRuby is an interpreter: it reads your program, translates it into instructions for its virtual machine and carries them out one by one. Because it is always there while the program runs, a program may change itself on the way – with eval, define_method or method_missing.
Since 2024 Matz has been writing something quite different: Spinel, an ahead-of-time compiler (its first version came out in September 2026). Spinel reads the whole program before it runs, works out which type every value has, and writes C from it. A C compiler turns that into machine code: a program with no Ruby left in it, which often runs much faster.
In this lesson all of that happens in your browser, as WebAssembly:
- spinel translates Ruby into C,
- clang, a C compiler, translates the C into WebAssembly,
- the finished program runs.
Spinel and clang are about 27 MB together and are loaded as soon as you open the lesson. That is why the cells here do not run while you type: one run of the compiler takes a few seconds. Press ▶.
def fib(n)
n < 2 ? n : fib(n - 1) + fib(n - 2)
end
puts fib(30)In this lesson the code of a cell is a program for Spinel – you write perfectly ordinary Ruby, with nothing special around it. Below the cell you see what happens to it: the three steps with their times, what the finished program prints, and below that what CRuby prints for the same code. CRuby is the yardstick: Spinel should come out with exactly the same.
Compare the times. Compiling takes seconds; the finished program is done in a few milliseconds – look how long CRuby takes for fib(30). A compiler does its work once, beforehand, so that the program is fast afterwards. Open the C as well: fib has become the C function sp_fib, and n is an sp_int, a number the processor computes with directly – Spinel saw that only Integers arrive here. main.wasm is the finished program, to download.
class Cat
attr_reader :name
def initialize(name, age)
@name = name
@age = age
end
def introduce
"I'm #{@name} and #{@age} years old."
end
end
cats = [Cat.new("Mimi", 3), Cat.new("Felix", 7)]
cats.each { |cat| puts cat.introduce }
p cats.map(&:name).sortSpinel worked out that @name is always a String and @age always an Integer. In the C a cat is therefore a struct sp_Cat_s with exactly these two fields. This is called type inference: you write no types down, Spinel reads them off the whole program.
What can only be known while the program runs, Spinel refuses: eval with a String (it could contain anything), method_missing, define_method with a computed name, ObjectSpace, binding as an object. Spinel says so while compiling, with the line:
sum = "6 * 7"
puts eval(sum)CRuby works out 42, Spinel refuses. One peculiarity comes from WebAssembly: here an Integer has only 32 bits, so at most 2,147,483,647. Past that Spinel raises a RangeError where CRuby simply switches to a big number; on your computer Spinel computes with 64 bits. Threads and fibers do not exist in WebAssembly either.
And IRB? You know IRB from lesson 47: it takes a line, runs it, shows => and waits for the next one. That works because CRuby is there while the code runs. A compiled program has no eval – so there is really no IRB for Spinel. The IRB below cheats honestly: every line is compiled together with all the lines before it into a new program and run from the start, and only what the new line prints is shown. That is why every line takes a few seconds. Try x = 6 * 7, then def double(n) = n * 2, then double(x).
show_spinel_irbAs a program for your computer
So far Spinel compiled and ran the programs inside this page. But the real point of a compiler is a standalone program: a file you can hand to someone, which runs without Ruby. That is what the 📦 Program button under this lesson's cells does: it builds the cell's code as a Windows .exe or as a Linux program. That also happens right here in the browser: Spinel, clang and the linker run as WebAssembly in your tab, nothing is uploaded. The first time, the page loads the compiler for it (about 45 MB); after that it is cached.
You do not have to write anything special for this: every cell of this lesson has the button, and the code is built just as it stands in the cell – the same program that ▶ has Spinel translate above. Try it with this cell: ▶ lets Spinel and CRuby run it, 📦 turns it into a program.
# A perfectly ordinary program: the primes up to 50
def prime?(n)
return false if n < 2
i = 2
while i * i <= n
return false if n % i == 0
i += 1
end
true
end
puts (1..50).select { |n| prime?(n) }.join(" ")The file is linked statically and needs nothing else. On Linux: chmod +x program and ./program. On Windows: double-click it, or run program.exe in a terminal (Windows may warn about an unknown .exe – but you just built it yourself). The same limits apply as to any Spinel program: only part of Ruby, and this page's helpers like show_image do not exist in it. If you do not see the button, this server does not offer the compiler.
Spinel is a C program: git clone https://github.com/matz/spinel, then make deps and make in it. After that ./spinel app.rb compiles a file into a program ./app that runs without Ruby. For bigger projects there is spin: spin new myproject, spin run and spin test, which takes CRuby as the yardstick, as here. The main.wasm from above runs with wasmtime: wasmtime run -W exceptions=y main.wasm.
collatz(n) that counts how many steps it takes to reach 1, and let Spinel print collatz(27). (It is 111 – and on the way the number grows as big as 9232.)# even: n / 2, odd: 3 * n + 1 - until n == 1
def collatz(n)
steps = 0
steps
end
puts collatz(27)