Methods
Learn Ruby method syntax, implicit returns, default and keyword arguments, splat arguments, and the ? and ! naming conventions.
Introduction
Ruby methods share the readability focus of everything else in the language — implicit returns, expressive argument styles, and naming conventions (like a trailing ? or !) that communicate intent directly through a method's name.
- How to declare methods, and how Ruby returns a value without an explicit return keyword.
- How default values and keyword arguments make method calls more flexible and readable.
- How splat arguments (*args) accept a variable number of arguments.
- What the ? and ! suffixes conventionally signal about a method.
Declaring Methods
def greet(name) "Hello, #{name}!"end
puts greet("Divya")Click Run to see what this code prints.
Implicit Returns
Notice greet above has no return keyword — in Ruby, a method automatically returns the value of the last expression it evaluates. An explicit return still works and is used for early exits, but is optional for a method's final line.
def classify(number) if number > 0 "positive" elsif number < 0 "negative" else "zero" endend
puts classify(-5) # negativeputs classify(0) # zeroClick Run to see what this code prints.
Default and Keyword Arguments
A parameter can have a default value, making it optional at the call site. Keyword arguments — name: value pairs — make calls with several parameters far more readable than relying purely on positional order.
def create_user(name, role: "member", active: true) "#{name} (#{role}) -- active: #{active}"end
puts create_user("Aditi")puts create_user("Rohan", role: "admin")puts create_user("Sara", role: "admin", active: false)Click Run to see what this code prints.
Splat Arguments
A parameter prefixed with * (a "splat") collects any number of extra positional arguments into an array — Ruby's equivalent of variadic parameters.
def sum(*numbers) numbers.reduce(0) { |total, n| total + n }end
puts sum(1, 2, 3, 4, 5)Click Run to see what this code prints.
Naming Conventions: ? and !
Ruby has two strong, widely followed naming conventions baked directly into method names: a trailing ? signals a method returns a boolean (a "predicate" method), and a trailing ! signals a method mutates its receiver in place or is otherwise more "dangerous" than its non-bang counterpart.
name = "ruby"
puts name.empty? # false -- a predicate methodputs name.upcase # RUBY -- returns a new string, "name" is unchangedputs name # ruby -- unchanged
name.upcase! # mutates "name" itself in placeputs name # RUBYClick Run to see what this code prints.
Ruby doesn't technically enforce what ? or ! mean — ? and ! are just legal characters at the end of a method name. But the community follows this convention so consistently that violating it (like naming a mutating method without a !) is considered a real style mistake.
Common Mistakes
- Assuming a ! method is always "more correct" to use than its non-bang counterpart — it specifically means "mutates in place" or "raises instead of returning nil," not simply "better."
- Forgetting that positional arguments must come before keyword arguments in a method signature.
- Writing an explicit return on every line out of habit — it's valid but not idiomatic for a method's final expression.
Best Practices
- Rely on implicit returns for a method's final value; reserve explicit return for early exits.
- Use keyword arguments once a method takes more than two or three parameters, for clarity at the call site.
- Follow the ? and ! naming conventions strictly in your own methods — other Ruby developers will rely on them being accurate.
Frequently Asked Questions
No — Ruby allows omitting them in many cases (puts "hi" instead of puts("hi")), though parentheses are often kept for methods with several arguments for clarity.
It's the common nickname for a method ending in ! — conventionally signaling that it mutates its receiver in place or behaves more riskily than its non-bang version (like raising an error instead of returning nil).
Yes, informally — Ruby doesn't have a distinct "multiple return" syntax, but returning an array (like return min, max, which is really return [min, max]) and destructuring it at the call site achieves the same result.
Key Takeaways
- A Ruby method implicitly returns the value of its last evaluated expression.
- Default values and keyword arguments make method calls more flexible and self-documenting.
- A *splat parameter collects any number of extra positional arguments into an array.
- A trailing ? conventionally signals a boolean-returning method; a trailing ! signals in-place mutation or added risk.
Summary
Ruby methods reward writing expressive, self-documenting call sites through keyword arguments and clear naming conventions. Next, you'll cover blocks, procs, and lambdas — Ruby's powerful, flexible way of passing chunks of code around as values.