Code

15 px

Ctrl + Enter Run · Ctrl + / Comment

gets reads the next line (gets.chomp drops its newline; gets.to_i makes it a number).

Ready

Output

Press Run to see the output here.
Saved on this deviceRuns in your browser · no networkLn 1, Col 1Spaces: 2Ruby

Online Ruby compiler

Write Ruby, give it input and run it, in your browser. This is the real Ruby 4.0 interpreter with its standard library, so programs behave as they do with the ruby command.

How to use the Ruby compiler

Write a program in the editor or pick one from Examples: input and arithmetic, arrays, a prime check, strings, a word count with a hash, recursion, a class, blocks and yield, exceptions, and modules with Comparable. Press Run or Ctrl+Enter. puts, print and p write to the output; warn and $stderr write in red.

Your code, input and tests are saved on this device as you type. Share makes a link with the code and input inside it, and Download saves the code as main.rb.

How input works

The Input tab is the program's standard input. gets reads the next line with its newline, so gets.chomp is the usual way to read text, gets.to_i reads a number and gets.split.map(&:to_i) reads several numbers from one line. STDIN.read reads everything left. When the input runs out, gets returns nil, and the compiler notes that the program wanted more input.

What runs your code

Ruby 4.0, the official interpreter (CRuby), compiled to WebAssembly by the ruby.wasm project and run in your browser with Ruby's standard library: require 'set', 'json', 'date', 'prime' and the rest work. Gems cannot be installed. The first run downloads Ruby once, about 10 MB, and your browser keeps it for next time. Errors are reported the way ruby reports them, with the line in main.rb and the error's class, such as NoMethodError.

Each run may take 10 seconds; then it is stopped. Output is kept up to 10,000 lines or 1 MB, and the rest is cut with a notice. Programs cannot reach the network, and no packages or libraries can be installed: what a language brings with it is what you can use. A loop that never ends is stopped at 10 seconds, keeping what it printed.

Questions

Which version of Ruby is this?
Ruby 4.0, the CRuby interpreter compiled to WebAssembly, with its standard library.
Why does gets.chomp fail with undefined method for nil?
The program read more lines than the Input tab has: gets returned nil. Add the missing lines of input and run again.
Can I install gems?
No. Everything in Ruby's standard library is available, but nothing can be downloaded while your program runs.
Is there a step-by-step visualizer for Ruby?
Not yet: Ruby runs on its own interpreter here, which the visualizer cannot follow. Python, C, C++, Java, JavaScript and TypeScript have one.

See also