Intern Track Roadmap

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
Free preview · Lesson 1 3 min read

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 lessons
Type to start searching...