Advertisement

Online Ruby Compiler

Write, run and test Ruby code online, right in your browser. This free online Ruby compiler needs no installation or signup — just type your code and click Run.

Ruby is a dynamic, expressive language designed for programmer happiness, best known for the Ruby on Rails web framework.

How to use the Ruby compiler

  1. Write or paste your Ruby code in the editor.
  2. If your program reads input, open the I/O tab and type it into the STDIN box (one value per line).
  3. Click Run — the output and any errors appear in the output panel.

Sample: read input in Ruby

name = gets.chomp
puts "Hello, #{name}!"

Paste this into the editor, type your name in the STDIN box and click Run.

More Ruby examples to try

Blocks and iterators

Ruby's blocks make collection processing short and readable.

scores = { "Asha" => 91, "Ravi" => 64, "Meena" => 85 }

scores.each { |name, s| puts "#{name.ljust(6)} #{s}" }
toppers = scores.select { |_, s| s >= 80 }.keys
puts "Toppers: #{toppers.join(', ')}"
puts "Average: #{scores.values.sum / scores.size.to_f}"

Classes and modules

A class that includes a module to share behaviour.

module Greet
  def greet
    "Hello, I am #{name}"
  end
end

class Person
  include Greet
  attr_reader :name

  def initialize(name)
    @name = name
  end
end

puts Person.new("Asha").greet

Common Ruby errors and how to fix them

undefined method `x' for nil:NilClass
You called a method on nil — often gets returned nil because no input was given. Add STDIN, or use &. safe navigation.
syntax error, unexpected end-of-input, expecting `end'
A block, method or if is missing its closing end.
undefined local variable or method `x'
The name is misspelled or defined in another scope.
String can't be coerced into Integer
Convert text before maths with .to_i or .to_f.

Frequently asked questions

Is this online Ruby compiler free?

Yes. It is completely free to use, with no signup required to write and run code.

Do I need to install anything?

No. It all works from your web browser, so there is nothing to download or set up — it works on Windows, macOS, Linux, Chromebooks and phones.

How do I give input to my Ruby program?

Before clicking Run, type the input into the STDIN box under the I/O tab, one value per line. Your program reads it as standard input, just like typing in a terminal.

Is this good for learning Ruby?

Yes. It is ideal for practising exercises, testing small programs and trying out ideas from tutorials, because you can run code instantly without configuring an environment.

Other online compilers

Editor powered by OneCompiler.