Methods and Return in Ruby
Fast recall
Quick revision
Core answer
def name(parameters) ... end. A method returns the value of its last evaluated expression unless return exits earlier. Parameters may be required, optional, keyword-based, collected with *args, or collected with **kwargs; Ruby 3 treats positional and keyword arguments separately.Whether you know def/end, implicit return (last expression), default/keyword/splat args, and ?/! naming.
Adding explicit `return` everywhere (Ruby returns the last expression), or using parens inconsistently.
Methods: def name(args); ...; end. Implicit return (last expression). Args: required, default (a=1), keyword (name:), *args, **opts. snake_case; ? predicates, ! dangerous.
Interview-ready guide
Interview answer
A Ruby method packages behaviour behind a name and is defined with def ... end. Every call produces a value. When execution reaches the end normally, Ruby returns the value of the last evaluated expression, so a final return is usually unnecessary.
An explicit return stops the method immediately. It is most useful for a guard clause such as return 0 if price.nil?, which handles an exceptional or empty case before the main path. return with no value returns nil; otherwise it returns the supplied object.
Parameters can be required or optional. Keywords such as tax: make the meaning visible at the call site. *args gathers extra positional arguments into an Array, while **options gathers extra keyword arguments into a Hash. The complete example combines an early guard, a keyword default, and an implicit result.
Ruby 3 treats positional arguments and keyword arguments as separate channels. A final positional Hash does not automatically satisfy a method's keyword parameters. Forward positional values with *args, keywords with **kwargs, and a block with &block, or use supported ... forwarding when the whole call should pass through unchanged.
Methods and Return in Ruby
Direct Answer
Define a Ruby method with def name(parameters) ... end. A method returns the value of its last evaluated expression unless return exits earlier. Parameters may be required, optional, keyword-based, collected with *args, or collected with **kwargs; Ruby 3 treats positional and keyword arguments separately.
Deep Dive & Architecture
When Ruby calls a method, it first matches the supplied positional and keyword arguments to the parameter list. Missing required inputs raise ArgumentError; optional defaults are evaluated when the method is called. Extra positional or keyword values are accepted only when the signature has the corresponding splat parameter.
The body is a sequence of expressions. The value of the final expression becomes the result, which may be an object, false, or nil. A guard clause uses return to stop before that final expression. This is useful for invalid or empty input because it keeps the main path from being nested inside a large conditional.
Blocks are not ordinary positional arguments. A method can yield to an attached block, ask whether one exists with block_given?, or capture it as a Proc through &block. Capturing has a cost and is unnecessary when the method only needs to yield immediately.
Ruby 3's keyword separation prevents ambiguous calls. A method declared with name: expects a keyword, while a method declared with one positional options parameter expects a Hash. Delegators should forward positional inputs with *args, keywords with **kwargs, and the block with &block or the ... forwarding syntax supported by the target Ruby version.
Code Example
def total(price, tax: 0.18)
return 0 if price.nil? # explicit early exit
price * (1 + tax) # implicit result
end
total(100, tax: 0.05) # => 105.0
def collect(first, second = 0, *rest, label:, **options)
{ first: first, second: second, rest: rest, label: label, options: options }
end
collect(10, 20, 30, 40, label: 'sample', cached: true)
# => { first: 10, second: 20, rest: [30, 40],
# label: 'sample', options: { cached: true } }
Common Mistakes & Pitfalls
Adding explicit return everywhere (Ruby returns the last expression), or using parens inconsistently.
Check your understanding
Answer these without looking back at the article.
- When would you use Methods And Return in a real project?
- How would you compare Methods And Return with an alternative?
- What is a common mistake when using Methods And Return?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.