RbRuby · Lesson 4 of 11
Methods
In Ruby, methods are defined with `def`. They implicitly return the last expression, which either feels elegant or terrifying — but saves a lot of typing.
Methods in Ruby don't need explicit return statements — the value of the last expression is automatically returned. A trailing ? means the method returns a boolean. A trailing ! means the method is destructive (modifies the receiver in place).
✦ Tip
The difference between Proc and Lambda: lambdas check argument count and `return` exits only the lambda. Procs are loose about arguments and `return` exits the enclosing method. In most cases, prefer lambdas (`->`) for anonymous functions you pass around.
The difference between Proc and Lambda: lambdas check argument count and `return` exits only the lambda. Procs are loose about arguments and `return` exits the enclosing method. In most cases, prefer lambdas (`->`) for anonymous functions you pass around.