Advanced Ruby and concurrency
This part looks under the language. Metaprogramming, pattern matching and the modern tour come first, then the object model and method lookup, method_missing and respond_to_missing?, define_method and small DSLs, refinements, garbage collection and profiling. The part ends with concurrency: threads and their locks, processes and signals, fibers and the scheduler, and parallel work without a framework.
12.1
Metaprogramming
Call methods dynamically and define them at runtime in Ruby.
Open
12.2
Pattern matching
Match arrays and hashes and bind variables with case-in.
Open
12.3
Modern Ruby tour
Tour endless methods, numbered block params, pattern matching, and Ruby 3 in production.
Open
12.4
The object model and method lookup
ancestors as the lookup order, prepend in front of a class, per-object singleton methods, and the exact path a call takes.
Open
12.5
method_missing done right
method_missing with respond_to_missing?, a settings object built from both, and the three cases where the pair is the wrong tool.
Open
12.6
define_method and small DSLs
define_method with a block, instance_variable_set for the storage, and a two-setting class DSL in fifteen lines.
Open
12.7
Refinements
refine and using, a String method that applies in one file, and the class that still cannot see it: scoped monkey patching.
Open
12.8
Garbage collection and memory
GC.stat before and after an allocation, ObjectSpace counts, and the string churn that shows up in every profile.
Open
12.9
Profiling a slow script
Benchmark two implementations, find the quadratic string concatenation, fix it, and read the stackprof report that points at the same line.
Open
12.10
Pattern matching in depth
in with hashes and arrays, find patterns that match a middle element, guards, and the two methods a class needs to be a pattern.
Open
12.11
Threads and fibers
Run code concurrently with threads, understand the GVL, share state safely with Mutex, and pause work with fibers.
Open
12.12
Threads, mutexes and queues
A mutex around shared state, Queue for handing work over, a producer and consumer that finish cleanly, and ConditionVariable for a wait.
Open
12.13
Processes, fork and signals
fork, Process.wait and $?, a signal handler that talks to a parent, and why a process beats a thread for CPU work on CRuby.
Open
12.14
Fibers and the scheduler
Fiber.new and resume, Fiber.yield with a value, the shape of a fiber scheduler, and the concurrency a fiber does not give you.
Open
12.15
Parallel work without a framework
A worker pool over Queue with four threads, fork for CPU-bound jobs, and one Ractor example with its experimental warning.
Open