Introduction
A .rb file holds source code in Ruby,
the dynamic, object-oriented language created by Yukihiro Matsumoto and first released to
the public in 1995. A Ruby program is plain text, read and run by the ruby
command with no separate compile step.
The same language is used in several other kinds of file, told apart by extension or by name:
| Name | What it holds |
|---|---|
.rb | Ruby source: a program, or a library loaded with require |
.rbw | Ruby source on Windows, associated with rubyw, the Ruby executable that opens no console window |
.rake | Tasks for Rake, Ruby’s build tool |
Rakefile | The main Rake file of a project, found by name |
.gemspec | The specification of a RubyGems package |
Gemfile | A project’s gem dependencies, read by Bundler |
.ru | A Rack web application configuration, usually config.ru |
All of these are ordinary Ruby: each is run by a Ruby interpreter, in a context that makes some extra methods available.
Structure
A Ruby file is text, in UTF-8 unless a comment on its first line says otherwise. There is
no required shape and no entry point: expressions run from the top of the file to the
bottom, and class and method definitions are themselves expressions that run as they are
reached. A line consisting of __END__ ends the program, and anything after it
is data the program can read.
#!/usr/bin/env ruby
# frozen_string_literal: true
require "json"
class Greeter
def initialize(name)
@name = name
end
def greet = "Hello, #{@name}"
end
puts Greeter.new("world").greet
What this page does not cover
This overview is the only page of the reference so far. It describes the kinds of file
that hold Ruby and their overall layout. It does not yet describe the language itself:
lexical structure, literals, types, variables, expressions, control flow, methods, blocks,
classes and modules, refinements, exceptions, pattern matching, regular expressions, loading
and gems, concurrency, the standard library, the versions of Ruby, or the contents of
Rakefile, Gemfile, .gemspec and .ru files. Until those pages
exist, the Ruby syntax
documentation is the place to read about them.
Links
Software
- Ruby – the reference interpreter, with the
ruby,gem,rakeandirbcommands (Windows, macOS, Linux) - Visual Studio Code – free code editor with Ruby extensions (Windows, macOS, Linux)
- RubyMine – JetBrains’ development environment for Ruby (Windows, macOS, Linux)
- Vim – text editor with Ruby syntax support (Windows, macOS, Linux)
Developers
- Prism – portable, error-tolerant parser for Ruby source, shipped with Ruby (C, Ruby)
- Ripper – Ruby’s standard library for parsing Ruby programs into S-expressions (Ruby)
- tree-sitter-ruby – Ruby grammar for the Tree-sitter parser used by editors
Reference
- Ruby syntax documentation – the official description of the syntax, for Ruby 4.0
- Ruby 4.0 documentation – the official documentation of the core classes and standard library