Language cheatsheet
Ruby 3 Cheatsheet
Run Ruby code, manage gems with Bundler, and use blocks, collections, classes, pattern matching and error handling.
Command line and gems
- Check the Ruby version — Print the installed Ruby version.
- Run a script — Run a Ruby file from the command line.
- Run a one-line command — Evaluate code from the command line.
- Open the interactive shell — Start IRB, the interactive Ruby shell.
- Install a gem — Install a library from RubyGems.
- Start a Gemfile — Create a Gemfile for a project's dependencies.
- Install the bundle — Install the gems listed in the Gemfile and record exact versions in Gemfile.lock.
- Run a command with the bundle — Run a command using the gem versions in Gemfile.lock.
- Lint the code — Check style and common mistakes with RuboCop. Add -a to fix what is safe.
Basics
- Print and interpolate — Print a line, inserting expressions with #{}. Double quotes interpolate; single quotes don't.
- Use hashes and symbols — Hashes map keys to values. Symbols like :name are cheap, immutable names.
- Transform arrays — Arrays have rich methods, and most take blocks.
- Loop with an index — Iterate with each_with_index, or each_with_object to build a result.
- Branch — Use if, unless and the ternary operator. Only nil and false are falsy.
- Define a method with keyword arguments — Keyword arguments make calls self-documenting. The last expression is returned.
- Handle nil safely — &. calls a method only if the receiver is not nil.
- Common string methods — Strings have many helpers.
Blocks, classes and pattern matching
- Write a method that takes a block — yield runs the block the caller supplied. Pass data to the block as arguments.
- Define a class — A class with an initializer, readers and a method.
- Share behavior with a module — Include a module to add its methods to a class. Including Comparable gives you < > == from <=>.
- Define simple data classes — Data.define creates immutable value objects (Ruby 3.2+); Struct is the mutable version.
- Match patterns — case/in destructures arrays and hashes and binds variables.
- Create a lambda — A lambda is a callable object that checks its arguments.
Errors and files
- Rescue exceptions — Handle errors with begin/rescue/ensure. Rescue specific classes, not Exception.
- Raise a custom error — Subclass StandardError for your own exceptions, then raise and rescue it.
- Read a file — Read a whole file, or iterate lines without loading it all.
- Write a file — Write text, with the file closed automatically at the end of the block.
- Work with JSON — Convert between Ruby hashes and JSON text.
- Use a Set — A collection of unique values with fast membership tests.
- Write a test — A Minitest test, which ships with Ruby.
Related cheatsheets
- C++ 23 cheatsheet — Compile and debug C++ with g++ and CMake, and use modern C++ (up to C++23): containers, algorithms, smart pointers, templates and threads.
- C# 14 cheatsheet — Modern C# 14 syntax for strings, nullability, collections, LINQ, records, pattern matching, async code and generics.
- CSS 3 cheatsheet — Layout with flexbox and grid, responsive design, custom properties, animations and modern selectors.
- Go 1 cheatsheet — The go command for modules, building, testing and cross-compiling, plus the code patterns you write every day.
- HTML 5 cheatsheet — HTML5 page structure, text, links, images, forms, tables and metadata, with accessible patterns.
- Java, Maven and Gradle 25 cheatsheet — JDK commands to compile, run and diagnose Java programs, plus the Maven and Gradle commands for builds and tests.