def: naming a reusable piece of logic

Free preview 3 min to read

Something you've been reading all along

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.

The basic shape

def greet
  puts "Hello!"
end

greet
# Hello!

def, a name, a body, end. Calling the name, greet, runs the body in order, once per call. Lesson 4 of the variables course used this to show scope without spelling out the syntax.

Why methods exist: naming repetition away

A method gives a name to logic you'd otherwise write out every time you need it.

# Without a method: repeated, identical logic
puts "Welcome, valued customer!"
# ... later, same message needed again ...
puts "Welcome, valued customer!"

# With a method: written once, called anywhere
def welcome_message
  puts "Welcome, valued customer!"
end

welcome_message
welcome_message

Change the message later and the method version has one place to edit; every call picks it up. Without it, each copy needs its own edit, and it's easy to miss one.

Calling with and without parentheses

Ruby accepts a call with or without parentheses, and both work the same:

greet()
greet

Ruby code usually drops the parentheses when there are no arguments, as above, and uses them once there are arguments, the next lesson's subject. Neither is wrong. It's a community convention, one of the expressive alternatives from Ruby: programmer happiness.

Naming methods: the same rules as variables

Method names follow the variables course's naming rules: snake_case, starting with a lowercase letter. A method that answers yes or no ends in ?, like empty?, zero? and passing?. You've read calls to all of them, just never their def.

def valid_email?(text)
  text.include?("@")
end

Nothing new syntactically. ? is legal at the end of a method name, and the community saves it for methods that return a boolean.

What this course covers

  • Lesson 1, def, naming reusable logic: the basic shape, and why methods exist at all (you are here)
  • Lesson 2, Parameters: feeding values into a method, and what happens when the count doesn't match
  • Lesson 3, Default parameter values: making an argument optional
  • Lesson 4, Implicit return: the value a method hands back, with no return keyword required
  • Lesson 5, The return keyword: exiting a method early, on purpose
  • Lesson 6, Keyword arguments: naming values at the call site instead of relying on position
  • Lesson 7, Putting it together: a small challenge combining everything

By the end, every def you've read without context will make sense, and you'll write your own.

Lesson 1 of 7 · free

Keep going with lesson 2

Parameters: feeding values into a method

Explains parameters as local variables filled by position, and the ArgumentError raised when the argument count is wrong.

A free account also gives you the quizzes, your progress and notes, and the discussion.

Type to start searching...