Learn Ruby

Expressive, object-oriented language designed for programmer happiness

24 lessons · 5 hours · 4 levels · covers Ruby 3.4

Ruby is a dynamic, fully object-oriented language with a clean syntax built around blocks and message passing. It powers Ruby on Rails and tools like Homebrew, Jekyll and Chef, and is loved for scripting and DSLs. Learning Ruby teaches you a flexible object model and an idiomatic, collection-oriented style that carries over to many languages.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics8 lessons · 2 hours in total
2. PracticalUse it productively at work, with the usual idioms15 lessons · 3 hours in total
3. AdvancedThe less common features, performance and trade-offs20 lessons · 4 hours in total
4. ExpertInternals and the edge cases experts know24 lessons · 5 hours in total

The Ruby tree

RubyRunning RubyRunning RubyVariables, numbers and nilVariables, numbersand nilConditionals, loops and caseConditionals, loopsand caseStrings and symbolsStrings and symbolsArrays, hashes and rangesArrays, hashes andrangesDefining methodsDefining methodsBlocks and iteratorsBlocks and iteratorsClasses and objectsClasses and objectsEnumerable in depthEnumerable in depthModules and mixinsModules and mixinsExceptionsExceptionsProcs, lambdas and closuresProcs, lambdas andclosuresPattern matchingPattern matchingGems and BundlerGems and BundlerFiles, IO and the standard libraryFiles, IO and thestandard library
Essentials and Practical shown (15 of 24 lessons); the full list is below. Click a lesson to read it.

All 24 lessons, in order

Language basics

  1. Running Ruby · Essentials · 8 min

    Run Ruby scripts and IRB, print with puts and p, and meet Ruby's everything-is-an-object idea.

  2. Variables, numbers and nil · Essentials · 10 min

    Assign variables and work with Integer, Float, String, Symbol, true, false and nil, including Ruby's truthiness rule.

  3. Conditionals, loops and case · Essentials · 10 min

    Branch with if, unless and case/when, use modifier forms, and loop with while, until and times.

  4. Strings and symbols · Essentials · 10 min

    Interpolate and manipulate strings, understand mutability and frozen literals, and know when to use symbols.

  5. Defining methods · Essentials · 12 min

    Define methods with positional, optional, keyword and splat arguments, and rely on implicit returns.

Collections and blocks

  1. Arrays, hashes and ranges · Essentials · 12 min

    Create and access arrays, hashes and ranges, including default values and symbol-key shorthand.

  2. Blocks and iterators · Essentials · 14 min

    Pass blocks to each, map and select, and write your own iterators with yield.

  3. Enumerable in depth · Practical · 14 min

    Use Enumerable's power tools: reduce, group_by, partition, tally, each_with_object, sort_by and friends.

  4. Procs, lambdas and closures · Practical · 14 min

    Turn blocks into objects with Proc and lambda, understand their differences, and pass methods around.

  5. Enumerators and lazy evaluation · Advanced · 14 min

    Build custom Enumerators, iterate externally with next, and process infinite or huge sequences lazily.

Objects and modules

  1. Classes and objects · Essentials · 14 min

    Define classes with initialize, instance variables and attr_accessor, and customise to_s and comparison.

  2. Modules and mixins · Practical · 14 min

    Use modules as namespaces and mixins with include, extend and prepend, and implement Comparable and Enumerable.

Practical Ruby

  1. Exceptions · Practical · 12 min

    Raise and rescue exceptions, use ensure and retry, and design a custom error hierarchy.

  2. Pattern matching · Practical · 12 min

    Destructure arrays and hashes with case/in, guards, pins and find patterns, and use => and in as one-liners.

  3. Gems and Bundler · Practical · 12 min

    Install libraries as gems, declare dependencies in a Gemfile, lock them with Bundler and run with bundle exec.

  4. Files, IO and the standard library · Practical · 12 min

    Read and write files safely with blocks, work with paths, and parse JSON and CSV from the standard library.

  5. Testing with Minitest and RSpec · Advanced · 14 min

    Write tests with Minitest's assertions and RSpec's describe/it/expect style, and stub collaborators.

Metaprogramming

  1. The object model · Advanced · 16 min

    Understand singleton classes, method lookup through ancestors, self, and how class methods really work.

  2. Metaprogramming · Advanced · 16 min

    Generate methods with define_method, call them dynamically with send, and handle unknown calls with method_missing.

  3. Building DSLs with blocks · Advanced · 14 min

    Combine blocks, instance_eval and method chaining to build readable internal DSLs like Rails routes and RSpec.

Runtime and performance

  1. Threads, the GVL and Ractors · Expert · 18 min

    Use threads, Mutex and Queue for I/O concurrency, understand the GVL, and know what Ractors offer for parallelism.

  2. Fibers and the fiber scheduler · Expert · 16 min

    Use Fibers as lightweight coroutines and understand how the fiber scheduler enables non-blocking I/O.

  3. Memory, GC and object shapes · Expert · 16 min

    Learn how CRuby allocates objects, how its generational GC works, and how to find allocation hot spots and leaks.

  4. YJIT and performance tuning · Expert · 14 min

    Enable YJIT, benchmark correctly, profile with stackprof or vernier, and write code the JIT can optimise.