Skip to content
Devtoolry

Ruby Compiler

Run Ruby 4.0 code online with the standard library and stdin input. CRuby runs in your browser as WebAssembly, so your code never leaves your device.

Something not working, or missing a feature? Send feedback

About the Ruby Compiler

The online Ruby Compiler runs your code with CRuby 4.0, the reference Ruby implementation, compiled to WebAssembly with the standard library included. Test a method, try a new syntax feature or check a Struct, Set or JSON snippet without installing Ruby; the interpreter runs in your browser, so your code stays on your device.

Your code runs in an isolated Web Worker in your browser, so it never leaves your device.

How to use the Ruby Compiler

  1. Write or paste Ruby code.
  2. Put the lines your program reads with gets or STDIN.read in the stdin box.
  3. Click Run. The first run downloads Ruby (about 9 MB); later runs start in under a second.
  4. Read the output, or the error with its line number in main.rb.

Example

Count the most frequent words

Input

counts = STDIN.read.downcase.scan(/\w+/).tally
counts.sort_by { |word, n| [-n, word] }.first(3).each do |word, n|
  puts "#{word}: #{n}"
end

Result

the: 3
cat: 1
dog: 1

With "the cat saw the dog" and "the end" in the stdin box. tally counts each word, and sorting by [-n, word] puts ties in alphabetical order.

Common mistakes

  • gets returns nil at the end of input. Once the stdin box is used up, gets returns nil, so gets.chomp raises NoMethodError. Use gets&.chomp, or loop with while (line = gets).
  • puts, print and p. puts adds a newline and prints arrays one element per line; print adds nothing; p shows the inspected value, with quotes around strings. Use p when you need to see exactly what a value is.
  • Requiring a gem. Only the standard library is installed, so require "nokogiri" or "rails" fails with LoadError. Try the logic with standard classes such as Set, Struct, Data and JSON.

Works well with the Ruby Compiler

Frequently asked questions

Which Ruby version is it?

CRuby 4.0 (the RUBY_VERSION constant prints 4.0.0), so pattern matching, endless methods, Data, the it block parameter and the Prism parser are all available.

Can I use gems?

The standard library is included, so require "json", "set", "date", "time" and "securerandom" work. Gems from rubygems.org cannot be installed in the sandbox.

Is my code sent to a server?

No. The Ruby interpreter runs in your browser as WebAssembly, so the code and its input stay on your device.

Is it a compiler or an interpreter?

An interpreter, like the ruby command: CRuby compiles your code to bytecode and runs it immediately. "Ruby compiler" is simply what most people search for.