def
Defines a method (function).
Syntax
def name(param) ... end
Description
Defined with def. Ruby automatically returns the value of the last expression (no need to write return, though you can).
Example
Loading editor…
Expected output
25
When to use?
Preventing code repetition.
Common mistakes
- ⚠️ Don't forget end. return is optional; the last line is returned anyway.