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
Not described
Known, not yet covered
- Reference: this overview is the only page of the reference. It describes the kinds of file that hold Ruby and their overall layout, not the language: 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 and the versions of Ruby. The Ruby syntax documentation describes them.
- Reference: the contents of
Rakefile,Gemfile,.gemspecand.rufiles (the methods each one makes available) are not described, and neither are the other implementations of Ruby. - Viewer: a
"…"or`…`string is read with its#{ … }parts, which may hold strings of their own ("a #{ "b #{1}" }"is one string, to 32 levels deep); the other string forms (%Q, heredocs, symbols such as:"a b") are not read for nested interpolation. Numbers are read as0b11,0o17,0x1F,1_000and with ther,iandrisuffixes (3r,2i); a constant after::is a type colour, not a symbol. - Viewer: variables inside
#{ … }are not coloured as code, method names afterdefand in calls are plain unless they are one of a short list (puts,require,attr_reader), the?acharacter literal is plain, and everything after__END__is coloured as a comment although it is data. - Viewer: the file is not parsed or run, so there is no outline or list of problems, and only the first 1 MiB is read and coloured. The methods that
Rakefile,Gemfileand.gemspecfiles add (task,gem,spec.name) are plain.
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