Methods
Ruby is a dynamic, beginner-friendly programming language, and methods let you package logic into named, reusable units you can call again and again.
Learn Methods in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll define methods, pass parameters with defaults and keywords, and understand Ruby's implicit return.
What You'll Learn in This Lesson
1️⃣ Defining and Calling Methods
You define a method with def , a name, optional parameters in parentheses, and a closing end . The big Ruby idea: the method returns its last evaluated expression automatically — no return needed. Parameters can have default values so callers may omit them.
2️⃣ Explicit Return, Keyword Args, and Naming
Reach for return only to exit early — like a guard for negative input. Keyword arguments (with a trailing colon) make calls read like labelled forms and free you from remembering argument order. And the trailing ? convention marks methods that answer true/false.
Your turn. Two area methods are written for you — you just need to call them with real numbers. Replace the TODO arguments, then run it.
Mini-Challenge: A Greeting Machine
Write a greet method that uses a keyword argument formal: to switch between two messages. Run with ruby hello.rb .
Common Errors (and the fix)
- "wrong number of arguments" — You passed too few or too many. Give the method exactly what it declares, or add a default value.
- Returning nil unexpectedly — If the last line is puts ... , the method returns nil . End with the value itself.
- "missing keyword: :name" — A keyword argument without a default is required. Pass it as name: "Lin" .
- Forgetting end — Every def needs a matching end .
- Calling before defining — Define the method above the line that calls it; Ruby reads top to bottom.
Pro Tips
- 💡 Keep methods small: a method that does one thing is easier to name, test, and reuse.
- 💡 Splat for many args: def sum(*nums) collects all arguments into an array.
- 💡 Name by behaviour: end predicate methods in ? and mutating ones in ! .
📋 Quick Reference — Methods
Concept
Ruby Syntax
Define
def name(args) ... end
Default param
def f(x = 1)
Keyword arg
def f(name:)
Early return
return x if cond
Splat (many)
def f(*items)
Predicate name
def valid?
Frequently Asked Questions
🎉 Lesson Complete!
- ✅ Define methods with def ... end
- ✅ The last expression is returned implicitly
- ✅ Parameters can have default values
- ✅ Keyword arguments make calls self-documenting
- ✅ Use ? for predicates and ! for mutating methods
- ✅ Next lesson: Blocks — pass chunks of code to methods
Practice quiz
Which keywords open and close a method definition?
- function ... end
- def ... }
- def ... end
- method ... end
Answer: def ... end. A method is written def name ... end.
What value does a Ruby method return by default?
- the last evaluated expression
- nil
- the first argument
- true
Answer: the last evaluated expression. Ruby implicitly returns the last expression evaluated.
When do you need an explicit return?
- always
- to return a string
- never; it is illegal
- only to exit a method early
Answer: only to exit a method early. Use return mainly for early exits, like a guard clause.
What is the result of def add(a, b); a + b; end then add(2, 3)?
- nil
- 5
- 23
- an error
Answer: 5. The last expression a + b is returned automatically, giving 5.
How do you give a parameter a default value?
- def f(x = 1)
- def f(x: 1)
- def f(x => 1)
- def f(x ?= 1)
Answer: def f(x = 1). A positional default uses = like def f(x = 1).
How are keyword arguments written and passed?
- with an asterisk
- in square brackets
- with a trailing colon, like name:
- with a leading @
Answer: with a trailing colon, like name:. Keyword arguments use a trailing colon and are passed by name.
By convention, a method name ending in ? signals that it:
- mutates the object
- returns a boolean
- is private
- takes no arguments
Answer: returns a boolean. A trailing ? marks a predicate returning true or false.
By convention, a trailing ! on a method name signals it:
- returns nil
- is faster
- is deprecated
- is a dangerous or mutating version
Answer: is a dangerous or mutating version. A trailing ! warns of a mutating or more aggressive variant, like sort!.
Why might a method return nil unexpectedly?
- it has too many arguments
- its last line is a puts, which returns nil
- it uses keyword arguments
- it has a default value
Answer: its last line is a puts, which returns nil. puts and print return nil, so ending on them returns nil.
What does def sum(*nums) collect into nums?
- a single value
- a hash of keywords
- all the arguments as an array
- a block
Answer: all the arguments as an array. The splat *nums gathers all positional arguments into an array.
Continue this course
- Previous: Control Flow
- Next: Blocks