Ruby methods & returns
This course covers how Ruby methods are defined and called. It starts with def, method naming in snake_case, and the question-mark convention for boolean methods. It then explains positional parameters and the ArgumentError raised when the argument count does not match, followed by default parameter values. Later lessons cover implicit return from the last expression, why a method ending in puts returns nil, early exits with return and guard clauses, and keyword arguments. A final challenge combines these ideas in one discounted price method.
What you'll learn
- ✓Defining and naming methods with def, including the ? convention
- ✓Passing positional arguments and reading ArgumentError messages
- ✓Making parameters optional with default values
- ✓How implicit return uses the last evaluated expression
- ✓Exiting early with return and writing guard clauses
- ✓Using required and optional keyword arguments
def: naming a reusable piece of logic
def has appeared in nearly every example since Ruby variables & assignment: calculate_total, BankAccount, symbolize_keys, average. Each time it demonstrated something else, like scope or symbol conversion. This course makes def the subject: how to define a method and what happens when you call it.
Read the full lesson free →Curriculum
7 lessonsdef: naming a reusable piece of logic
Introduces the def syntax, why methods remove repetition, optional parentheses, and snake_case names ending in ? for booleans.
Parameters: feeding values into a method
Explains parameters as local variables filled by position, and the ArgumentError raised when the argument count is wrong.
Default parameter values
Shows how a default value makes a parameter optional, and why passing nil or false skips the default.
Implicit return: the last evaluated expression
Covers how a method returns its last expression, including inside branches, and why ending with puts returns nil.
The return keyword: exiting early, on purpose
Explains return as an immediate exit, the guard clause pattern, bare return giving nil, and when to use each style.
Keyword arguments: naming values at the call site
Introduces keyword arguments passed by name in any order, with required and optional forms and their error messages.
Putting it together: a small challenge
Reads a discounted_price method that combines keyword arguments, a guard clause and implicit return, then asks you to write it.