#!/usr/bin/ruby

# Program to find the factorial of a number
def fact(n)
    if n == 0
        1
    else
        n * fact(n-1)
    end
end

puts fact(ARGV[0].to_i)

class Range
  def to_json(*a)
    {
      'json_class'   => self.class.name, # = 'Range'
      'data'         => [ first, last, exclude_end? ]
    }.to_json(*a)
  end
end

{:id => 34, :key => "value"}


    herDocs = [<<'FOO', <<BAR, <<-BAZ, <<-`EXEC`] #comment
  FOO #{literal}
FOO
  BAR #{fact(10)}
BAR
  BAZ indented
    BAZ
        echo hi
    EXEC
puts herDocs

About This Ruby Editor

Ruby was designed by Yukihiro Matsumoto around programmer happiness, and it shows in the details: everything is an object, blocks are first-class, and the standard library reads close to English. Rails made it the default choice for a generation of web applications.

The editor highlights blocks, symbols, string interpolation and heredocs, and indents do/end and if/end pairs.

Ruby syntax reference

Ruby syntax reference
Syntax What it does
items.each { |item| puts item } Block with braces - conventionally for single-line blocks.
items.map do |item|\n item * 2\nend Block with do/end - conventionally for multi-line blocks.
"Hello #{name}" String interpolation. Only in double-quoted strings.
:symbol An immutable, interned identifier. Cheaper than a string as a hash key.
{ name: "Ada", age: 36 } Hash with symbol keys - the modern shorthand for { :name =&gt; "Ada" }.
def greet(name:, greeting: "Hi") Required and optional keyword arguments.
value = hash.dig(:a, :b, :c) Safe nested lookup; returns nil instead of raising.
attr_reader :name Generates the accessor method. attr_accessor generates both.

Ruby-specific surprises

  • Only false and nil are falsy. 0 and "" are both truthy, unlike most other scripting languages.
  • Bang methods mutate. sort returns a new array; sort! changes the receiver and returns nil if nothing changed - which is a frequent source of accidental nils.
  • Implicit return. A method returns its last expression. An explicit return inside a block returns from the enclosing method, which is rarely what you meant.
  • Symbols are never garbage collected before 2.2, so dynamically creating symbols from user input in an old codebase is a memory leak.
  • Frozen string literals. Add the magic comment # frozen_string_literal: true at the top of files; it is the default direction of the language.

Your Code Never Leaves Your Browser

The editor is JavaScript running in this tab. Nothing you type is posted to a server, stored, logged or associated with you, and there is no account and no autosave. That makes it safe to paste a snippet from a private codebase in order to read it with highlighting. The trade-off is that nothing persists either — refresh the page and your content is gone, so copy anything you want to keep.

Frequently Asked Questions

Can I run Ruby code in this editor?

No - there is no interpreter attached. This is an editor for reading and formatting a snippet; nothing is executed and nothing is uploaded.

What is the difference between a symbol and a string?

A symbol is immutable and interned, so every occurrence of :name is the same object in memory. A string is mutable and each literal creates a new object. Use symbols for identifiers such as hash keys, strings for text.

Is Ruby only for Rails?

No, though Rails is why most people meet it. Ruby is widely used for scripting, automation, and developer tooling - Homebrew, Vagrant, Chef and Jekyll are all Ruby.

Editors for Other Languages

Same editor, configured for a different language, each with its own syntax reference.