liminfo

Ruby Reference

Free reference guide: Ruby Reference

29 results

About Ruby Reference

This Ruby Reference is a searchable cheat sheet covering the core syntax and idioms of the Ruby programming language. It spans six categories — Basics, Methods, Blocks, Classes, Modules, and Collections — with copy-ready code examples for every entry. Whether you are writing a Rails controller, a Rake task, or a standalone CLI script, this page gives you instant access to the patterns you reach for most often.

Ruby is famous for its expressiveness: blocks and iterators replace verbose loops, symbols lighten hash keys, and mixins let you share behavior without deep inheritance trees. The reference captures these Ruby-specific patterns — from the difference between Proc and lambda, to the ? and ! method naming conventions, to the three inclusion modes include/extend/prepend — so you can write idiomatic Ruby without constantly switching to ri or the official docs.

Every example is written for Ruby 3.x compatibility and follows community style conventions. The reference is organized to mirror the way developers actually think about Ruby code: start with variables and control flow, move to methods and closures, then classes and modules, and finally the rich Enumerable API that powers map, select, reduce, and their friends.

Key Features

  • Covers all variable scopes — local, instance (@), class (@@), global ($), and constants
  • Method syntax including default arguments, splat (*args), double splat (**kwargs), and keyword parameters
  • Block, Proc, and Lambda comparisons with yield and block_given? patterns
  • Class definitions with attr_accessor, inheritance, Struct, and self-based class methods
  • Module usage for both mixins (include/extend/prepend) and namespacing
  • Enumerable collection operations — map, select, reject, reduce, flat_map, group_by, zip, and sort_by
  • Hash manipulation methods including merge, keys, values, and conditional filtering
  • Real-world code snippets following Ruby community style guide conventions

Frequently Asked Questions

What Ruby version does this reference target?

All examples are compatible with Ruby 3.x and generally work with Ruby 2.7+. Syntax like the numbered block parameters (e.g., _1, _2) introduced in 3.0 is not used, so the examples remain broadly portable.

What is the difference between a Proc and a Lambda in Ruby?

Both are callable objects, but they differ in two ways. A lambda enforces arity — passing the wrong number of arguments raises an ArgumentError — while a Proc silently assigns nil to missing args. A return inside a lambda exits only the lambda; a return inside a Proc exits the enclosing method.

When should I use a symbol instead of a string?

Symbols are immutable and are stored once in memory, making them ideal for hash keys, method names, and any identifier that does not change at runtime. Use strings when you need mutation (gsub!, concat) or when the value is user-generated data.

How do include, extend, and prepend differ?

include adds module methods as instance methods, extend adds them as class (singleton) methods, and prepend inserts the module before the class in the method lookup chain, allowing you to override and call super to reach the original implementation.

What does the ? and ! convention mean on method names?

A trailing ? indicates the method returns a boolean (e.g., empty?, include?). A trailing ! warns that the method is destructive or raises an exception instead of returning nil — for example, sort returns a new array while sort! mutates the receiver in place.

How does yield work inside a method?

yield transfers control to the block that was passed to the method. The method can pass arguments to the block and receive the block's return value. Use block_given? to check whether a block was supplied before calling yield, avoiding a LocalJumpError.

What is the Enumerable module and why is it important?

Enumerable is a mixin that provides collection methods like map, select, reject, reduce, sort_by, and group_by. Any class that defines an each method and includes Enumerable gains dozens of iteration and transformation methods for free.

Can I use this reference for Ruby on Rails development?

Yes. Rails is built on Ruby, so all core language syntax covered here — blocks, classes, modules, collections — applies directly. The reference focuses on the language itself rather than Rails-specific APIs, making it a useful companion alongside the Rails documentation.