Ruby
Online Ruby Compiler
Your script runs with ruby main.rb on Ruby 3.4, or 4.0 if you pick it in Run settings, with the standard library. gets waits for you to type in the terminal, and print shows a prompt straight away because output isn’t buffered here.
Try it aboveHow Ruby runs here
- Versions
- Ruby 3.4 (default), 4.0
- Run
ruby main.rb- Limits per run
- 256 MB memory, 15 s of CPU time on half a core, 2 MB of output
- Time
- Up to 10 minutes a run (30 on a paid plan); waits 2 minutes for input (5 on a paid plan)
- Sandbox
- Each run gets its own Linux sandbox with no network access. Text files your program writes next to your code open as new tabs.
A Ruby script that reads input
This main.rb adds up expenses by category with a Hash that defaults to 0, then sorts and formats the result:
print "How many expenses? " count = $stdin.gets.to_i totals = Hash.new(0) count.times do |i| print "Expense #{i + 1} (category amount): " category, amount = $stdin.gets.split totals[category] += amount.to_f end sum = totals.values.sum totals.sort_by { |_, amount| -amount }.each do |category, amount| puts format("%-9s %7.2f %5.1f%%", category, amount, amount / sum * 100) end puts format("%-9s %7.2f", "total", sum)
Four expenses typed in; food appears twice and is added up:
$ ruby main.rb How many expenses? 4 Expense 1 (category amount): food 12.50 Expense 2 (category amount): rent 400 Expense 3 (category amount): food 7.25 Expense 4 (category amount): bus 2.80 rent 400.00 94.7% food 19.75 4.7% bus 2.80 0.7% total 422.55
Reading input in Ruby
- gets returns the line with its newline: use gets.chomp for text, gets.to_i or gets.to_f for one number, and gets.split.map(&:to_i) for several numbers on one line.
- Prefer $stdin.gets to a bare gets. Plain gets reads from ARGF, so once you set program arguments in Run settings, it tries to open them as files and fails with Errno::ENOENT.
- to_i never complains: "abc".to_i is 0 and "42abc".to_i is 42. When bad input should stop the program, use Integer(gets), which raises ArgumentError instead.
- $stdin.read, readlines and $stdin.each_line wait for the end of input, which this terminal never sends (there’s no Ctrl+D). Read a count first, or stop on an empty line: while (line = $stdin.gets.chomp) != "".
Your own files: require_relative
Run starts main.rb, or the Entry file set in Run settings. Other .rb files sit in the same folder; load them with require_relative and the name without .rb:
# money.rb module Money def self.split(total, people) = (total / people.to_f).round(2) end # main.rb require_relative "money" puts Money.split(100, 3) # 33.33
A plain require "money" fails with LoadError, because the current folder isn’t on Ruby’s load path. Text files your script writes, such as File.write("report.txt", ...), open as new tabs after the run.
What isn’t available here
- Gems from RubyGems. gem install and Bundler need the network, which runs don’t have, so Rails, Sinatra, Nokogiri and HTTParty are out.
- The standard library is there: json, set, date, time, securerandom, digest, open3 and the rest load with require.
- An irb prompt. The playground runs the whole file from top to bottom, so try expressions by printing them with p or puts.
- Web servers. A script can’t serve pages from here; output is text in the terminal.
Errors you’ll see
Ruby 3.4 quotes names with single quotes, so messages read undefined method 'upcase' where older tutorials show `upcase'.
- “undefined method 'upcase' for nil (NoMethodError)” A variable or hash lookup was nil. Check where it was set, or use &. (safe navigation): name&.upcase.
- “String can't be coerced into Integer (TypeError)” You added a number and text, such as 5 + gets. Convert the text with to_i or Integer().
- “no implicit conversion of Integer into String (TypeError)” The reverse: "Total: " + 5. Use interpolation: "Total: #{total}".
- “invalid value for Integer(): "abc" (ArgumentError)” Integer() got text that isn’t a whole number. Rescue it, or ask again.
- “No such file or directory @ rb_sysopen - 5 (Errno::ENOENT)” A bare gets with program arguments set. Use $stdin.gets.
- “cannot load such file -- money (LoadError)” require instead of require_relative for your own file.
For a NoMethodError deep in a chain of calls, Fix with AI under the failed run looks for where the nil came from and proposes a change as a diff.
Ruby 3.4 in practice
- it names a block’s single parameter: [1, 2, 3].map { it * 2 } is the same as map { |x| x * 2 }.
- Prism is the default parser in 3.4, so syntax errors are worded differently from older Ruby versions and point at the exact spot.
- tally, sum, minmax, each_slice and each_cons cover most counting and grouping: "banana".chars.tally gives {"b" => 1, "a" => 3, "n" => 2}.
- One-line methods need no end: def square(x) = x * x.
- Switch to Ruby 4.0 in Run settings to check your code against the newest release.
Questions
Which Ruby version does this use?
Ruby 3.4 by default (MRI, run as ruby main.rb). Ruby 4.0 is in the version list in Run settings.
How do I take user input in Ruby online?
Call $stdin.gets.chomp for text or $stdin.gets.to_i for a number, and type the answer in the terminal while the script runs.
Can I install gems or use Rails?
No. Runs have no network, so gem install and Bundler can’t fetch anything. The standard library works.
How do I use a second Ruby file?
Add it with the + next to the tabs and load it with require_relative "name" from main.rb.
Why does gets fail with “No such file or directory”?
Plain gets reads the program arguments as file names. Use $stdin.gets to read what you type.