FizzBuzz with a range
(1..15).each do |i|
word = ""
word += "Fizz" if (i % 3).zero?
word += "Buzz" if (i % 5).zero?
puts word.empty? ? i : word
end
Write Ruby and run it right here. Ruby runs inside your browser, so nothing you type is uploaded. The first run downloads the runtime.
Ready
# gets reads one line from the input box.
name = gets&.chomp || "world"
puts "Hello, #{name}!"
(1..3).each do |i|
puts "#{i} squared is #{i * i}"
end
Type the input before you run. Each gets call reads the next line.
Press Run or Ctrl+Enter to see the output here.Runs in your browser with Ruby 3.4 (ruby.wasm 2.10.1). Your code is not sent to a server.
Runtime: Ruby 3.4 (ruby.wasm 2.10.1)
Ruby runs as CRuby 3.4 compiled to WebAssembly by the ruby.wasm project (version 2.10.1), together with its standard library, so require "set", "json" or "date" works. The first run downloads about 8 MB from the jsDelivr CDN, and later visits load it from the browser cache. Every run starts a fresh Ruby VM, so classes and constants from the last run are gone. Exceptions are printed with their message, class and a backtrace pointing at main.rb.
Everything happens on your device. The code you write is never uploaded, logged or stored on a server. Each run stops after 10 seconds and keeps up to 100,000 characters of output, so an endless loop cannot freeze the page.
Gems cannot be installed, and this WebAssembly build of Ruby has no Thread, so code that starts threads or uses Timeout fails. There is no access to the network or to files on your computer. Programs that read input in a loop get nil once the input box has been read to the end.
Open an example in the editor above, change it and run it. Opening an example does not run it.
(1..15).each do |i|
word = ""
word += "Fizz" if (i % 3).zero?
word += "Buzz" if (i % 5).zero?
puts word.empty? ? i : word
end
numbers = [5, 3, 8, 1, 9, 2]
p numbers.select(&:even?)
p numbers.map { |n| n * n }
p numbers.sort.reverse
p numbers.each_slice(2).to_a
puts numbers.sum, numbers.minmax.inspect
ages = { "Ada" => 36, "Linus" => 21, "Grace" => 45 }
ages.sort_by { |_, age| age }.each { |name, age| puts "#{name}: #{age}" }
class Temperature
include Comparable
attr_reader :celsius
def initialize(celsius)
@celsius = celsius
end
def fahrenheit = celsius * 9.0 / 5 + 32
def <=>(other) = celsius <=> other.celsius
def to_s = format("%.1f°C / %.1f°F", celsius, fahrenheit)
end
temps = [30, -5, 18].map { Temperature.new(_1) }
puts temps.max
puts temps.sort.map(&:to_s)
text = $stdin.read.downcase
counts = text.scan(/[a-z']+/).tally
counts.sort_by { |word, n| [-n, word] }.first(3).each do |word, n|
puts "#{word}: #{n}"
end
Input
the cat and the dog. The end!
class NegativeError < StandardError; end
def parse_age(text)
age = Integer(text)
raise NegativeError, "age cannot be negative" if age.negative?
age
end
%w[42 -1 abc].each do |raw|
puts "#{raw} -> #{parse_age(raw)}"
rescue NegativeError, ArgumentError => e
puts "#{raw} -> error: #{e.message}"
end
Programs here do not stop and wait for you to type while they run. Everything your program reads comes from the input box, which you fill in before pressing Run.
gets reads the next line including its newline, so write gets.chomp — or gets&.chomp when the input might be empty. $stdin.read returns everything at once and $stdin.each_line goes through it line by line. At the end of the input, gets returns nil.
Your latest Ruby code and input are saved automatically in this browser, so they are still here when you come back. Nothing is saved on a server.
Share creates a link that carries your code inside the address itself, compressed after the # sign. Whoever opens it sees your code in the editor, and it only runs when they press Run.
| Keys | Action |
|---|---|
| Ctrl+Enter | Run the code |
| Ctrl+/ | Comment or uncomment the line |
| Tab · Shift+Tab | Indent or outdent the selection |
| Ctrl+F | Find and replace in the code |
| Ctrl+Z · Ctrl+Shift+Z | Undo / redo |
| Esc → Tab | Leave the editor with the keyboard |
On a Mac, use ⌘ instead of Ctrl.
Ruby 3.4 — the interpreter is CRuby, the standard implementation, built for WebAssembly by ruby.wasm 2.10.1. Newer syntax such as endless methods, pattern matching and numbered block parameters all works.
No. Only the standard library that ships with Ruby is available, and there is no way to install gems. Rails and other frameworks also need a server, which this site deliberately does not have.
WebAssembly in the browser gives this Ruby build no operating-system threads, so Thread.new raises NotImplementedError and libraries that depend on threads, such as Timeout, fail the same way. Fibers and ordinary blocks work.
Yes. There is no account, no sign-up and no daily limit. The only limit is that a single run stops after 10 seconds.
No. The Ruby runtime is downloaded into your browser and your code runs there, on your device. The site only serves the page and the runtime files; it never receives the code you write or the input you give it.
In this browser's local storage, one draft per language. It stays until you clear this site's data, and it is not synced to other devices. Use a share link to move code somewhere else.
Press Share and send the copied link. The code is compressed into the part of the link after #, which browsers never send to a server. Very long programs do not fit in a link, and the page will tell you when that happens.
Yes. It runs in any current mobile browser. On a slow connection the first run takes longer because the runtime has to be downloaded once.