Methods and blocks
A method is the unit of behaviour in Ruby, and a block is what makes a method feel like part of the language. This part covers methods and their arguments, blocks, procs and lambdas, the visibility of a method, keyword arguments and forwarding, closures built from blocks, callable objects, and recursion as the way a method calls itself.
5.1
Methods
Group reusable logic into Ruby methods with clear arguments.
Open
5.2
Blocks, procs, lambdas
Pass chunks of code to methods with blocks, procs, and lambdas.
Open
5.3
Arguments: splats and kwargs
Collect and forward arguments with splats and keyword arguments.
Open
5.4
public, protected and private
What each visibility means for a caller, how private works on self, module_function for a module, and why a test reaches a private method through behaviour.
Open
5.5
Keyword arguments and forwarding
Required and optional keyword arguments, a **rest that collects the unknown ones, and ... that forwards every argument to another method.
Open
5.6
Blocks, yield and closures
yield calls the block a method was given, block_given? asks whether there is one, &block names it, and a block remembers the variables around it.
Open
5.7
Procs, lambdas and method objects
A proc and a lambda look alike and differ in arity and return, &:sym turns a method name into a block, and a method object or a curried lambda is a callable value.
Open
5.8
Recursion in Ruby
A method that calls itself, a recursive walk over nested data and a directory tree, and the stack limit that turns a deep recursion into a SystemStackError.
Open