Skip to content
    Ruby PlaygroundRuby 3.4 · ruby.wasm
    Output

    Ruby Online Compiler: Run Ruby 3.4 in Your Browser

    This Ruby online compiler runs CRuby 3.4, the reference Ruby interpreter, compiled to WebAssembly by the ruby.wasm project. The first time you press Run, the page downloads the interpreter and its standard library, then evaluates your script in your own browser and prints everything written to $stdout and $stderr. Uncaught exceptions appear with their class, message and line number. Use it as an online Ruby compiler for practising blocks, classes and Enumerable methods, for testing a regular expression or a date calculation, or for sharing a snippet that someone else can run without installing Ruby.

    How to use the Ruby online compiler

    1. Write Ruby in the editor, or pick a program from the Samples menu.
    2. Press Run. The first run downloads and compiles the Ruby interpreter, which can take a few seconds on a slow connection; later runs reuse it.
    3. Read the output panel. puts, print, p, pp and warn all appear there, in the order your program wrote them.
    4. Run again as often as you like. Each run starts a new Ruby VM, so methods, classes and constants from the previous run are gone.
    5. Press Share to copy a link that stores your code in the URL.

    Features

    • CRuby 3.4 compiled to WebAssembly (ruby.wasm 2.8.1 (WebAssembly))
    • Standard libraries such as json, set, date, time and stringio
    • Captures $stdout and $stderr, including puts, print, p, pp and warn
    • Exceptions and syntax errors shown with class, message and line
    • A fresh interpreter for every run, so results never depend on an earlier run
    • The interpreter is fetched only when you first press Run
    • Samples covering classes, blocks, metaprogramming, StringIO and dates
    • Share links that store your code in the URL

    What works and what doesn’t

    • Only the libraries shipped with the interpreter are available. json, set, date, time and stringio load with require; bundled gems such as bigdecimal and anything from RubyGems do not, because there is no gem install step.
    • Threads and subprocesses are unavailable: Thread.new raises NotImplementedError, and system or backticks cannot start other programs.
    • There is no network access. require "net/http" fails with a LoadError, because the socket extension is not part of the WebAssembly build.
    • Standard input is empty, so gets returns nil. Put test input in a string or heredoc, and wrap it in StringIO when code expects an IO object.
    • Output written straight to the STDOUT or STDERR constants skips the output panel and goes to the browser's developer console. Use puts, print, p, pp, warn, $stdout or $stderr instead.
    • Ruby runs on the page's main thread, so a script that never finishes freezes the tab until the browser offers to stop it.
    • The first run downloads about 30 MB (roughly 8 MB compressed), so it takes noticeably longer than the runs after it.

    Example programs

    Comparable, endless methods and the it parameter

    Including Comparable and defining <=> gives a class sort, max and every comparison operator. The one-line def syntax needs Ruby 3.0 or later, and the implicit it block parameter needs Ruby 3.4. Expected output: ["3.2.9", "3.4.1", "3.10.0"], then 3.10.0 and true.

    class Version
      include Comparable
      attr_reader :parts
    
      def initialize(text)
        @parts = text.split(".").map(&:to_i)
      end
    
      def <=>(other) = parts <=> other.parts
      def to_s = parts.join(".")
    end
    
    versions = %w[3.4.1 3.10.0 3.2.9].map { Version.new(it) }
    puts versions.sort.map(&:to_s).inspect
    puts versions.max
    puts Version.new(RUBY_VERSION) >= Version.new("3.4")

    Counting words with scan and tally

    String#scan extracts the words, Enumerable#tally counts them, and sort_by with a negated count orders by frequency and then alphabetically. The output lists is (2), appearance (1) and body (1), followed by unique words: 14.

    text = <<~TEXT
      Ruby is simple in appearance, but is very complex inside,
      just like our human body.
    TEXT
    
    counts = text.downcase.scan(/[a-z']+/).tally
    top = counts.sort_by { |word, n| [-n, word] }.first(3)
    
    top.each { |word, n| puts "#{word.ljust(10)} #{n}" }
    puts "unique words: #{counts.size}"

    Exceptions, retry and an uncaught error

    retry runs the begin block again after a rescued exception, and warn writes to the same output panel. The last line raises ArgumentError on purpose so you can see how an uncaught exception is reported: its class, its message and the line in main.rb where it happened.

    require "json"
    
    class RateLimited < StandardError; end
    
    attempts = 0
    begin
      attempts += 1
      raise RateLimited, "try again" if attempts < 3
      puts JSON.pretty_generate({ status: "ok", attempts: attempts })
    rescue RateLimited => e
      warn "attempt #{attempts} failed: #{e.message}"
      retry
    end
    
    Integer("forty-two")

    Frequently asked questions

    Does this Ruby online compiler run real Ruby?

    Yes. It runs CRuby, the reference interpreter published on ruby-lang.org, compiled to WebAssembly by the official ruby.wasm project. Output comes from the interpreter itself rather than from reading your code, so core classes, blocks, pattern matching and metaprogramming behave as they do on your computer. RUBY_VERSION reports the exact release.

    Which Ruby version does the playground use?

    Ruby 3.4, currently release 3.4.1, from ruby.wasm 2.8.1 (WebAssembly). Print RUBY_VERSION or RUBY_PLATFORM to check. The platform is wasm32-wasi instead of something like x86_64-linux or arm64-darwin, which is how a script can tell that it is running on WebAssembly. The release is fixed by the interpreter file the page loads, so everyone runs the same one.

    Can I install gems or run Rails?

    No. There is no Bundler or gem install step, the interpreter has no network access, and native extensions cannot be compiled in the browser. Default libraries such as json, set, date, time and stringio load with require. Bundled gems like bigdecimal are missing, and Rails, which needs a server and a database, will not run.

    Why is the first run slower than the next ones?

    The interpreter and its standard library form a WebAssembly file of about 30 MB, roughly 8 MB when compressed in transit. It is downloaded only when you first press Run, compiled once and kept in memory, so later runs in the same tab only start a new Ruby VM, which usually takes well under a second.

    How are Ruby errors and exceptions shown?

    An uncaught exception ends the script. The output panel keeps everything printed before it, then shows the exception class and message, such as ZeroDivisionError: divided by 0, and the line in main.rb where it was raised. Syntax errors are printed the way Ruby 3.4's Prism parser reports them, with a pointer to the problem.

    Is my Ruby code sent to a server?

    No. Your program runs inside your browser tab and is not sent to WebFiddle to be executed; only the interpreter file is downloaded, from the jsDelivr CDN. The code leaves your device only if you press Share and send someone the link, because that link stores the program, compressed, in its URL.

    Embed this sandbox

    Paste this iframe into any web page and readers can run code without leaving it. The snippet travels in the URL, so there is nothing to host and no account to create. Open the sandbox, write your example, press Share, then append that link’s query string to the embed URL to preload it.

    <iframe src="https://webfiddle.xyz/embed/ruby" width="100%" height="480" loading="lazy"
            style="border:1px solid #ddd;border-radius:8px"
            title="WebFiddle ruby sandbox"></iframe>
    Preview the embed

    Related articles

    Related sandboxes

    Official documentation