Endless Methods in Ruby
Fast recall
Quick revision
Core answer
= instead of a body closed by end, for example def active? = status == :active. Ruby evaluates that expression when the method is called and returns its value just like a normal method. The form is useful for small predicates, calculated attributes, and simple delegation. It should not compress branching, multi-step work, or error handling into one dense expression, and assignment methods such as setters cannot use this shorthand.Whether you know endless methods (Ruby 3.0+) use `=` instead of end and are for one-line expressions.
Using an endless method for multi-statement logic (they must be a single expression), or expecting `end`.
Endless methods (Ruby 3.0+): `def f(x) = expr` — one-line, no end. For trivial getters/computed values. Must be a single expression.
Interview-ready guide
Interview answer
An endless method is Ruby's compact syntax for a method whose body is one expression. def total = subtotal + tax defines the same public method shape as a normal def total ... end; the value of the expression is returned when the method runs.
The form is well suited to a small predicate, computed property, or delegation. For example, def adult? = age >= 18 keeps a simple rule next to its name without an extra closing line, and def display_name = profile.name makes a thin delegation easy to scan.
“One expression” does not automatically mean “simple.” A long conditional, chained mutation, or rescue modifier can technically be squeezed into an expression while hiding important branches and side effects. Normal method syntax gives those steps room and is easier to debug.
Endless definitions have the usual method behaviour for arguments, visibility, and return values, but setter assignment methods are a syntax boundary. They also require a Ruby version that supports the feature, so a gem's supported-version range can rule them out.
I choose the shorthand only when the method name and its expression explain the whole operation immediately. If a comment is needed to decode the line, the regular form is the clearer design.
Endless Methods in Ruby
Direct Answer
An endless method is a single-expression Ruby method written with = instead of a body closed by end, for example def active? = status == :active. Ruby evaluates that expression when the method is called and returns its value just like a normal method. The form is useful for small predicates, calculated attributes, and simple delegation. It should not compress branching, multi-step work, or error handling into one dense expression, and assignment methods such as setters cannot use this shorthand.
Deep Dive & Architecture
Ruby added endless definitions as an alternative spelling for a method with a single expression. The method is still installed on its class or module, receives arguments according to the normal method rules, respects public or private visibility, and runs only when called. The expression after = is the body; its result is the method result unless control flow inside that expression says otherwise.
This makes the syntax a natural fit for tiny queries. A predicate such as def empty? = size.zero?, a calculation such as def gross = net + tax, or a delegation such as def email = account.email has one idea and no setup. Keeping the name and rule on one line can improve scanning when a value object has several similarly small operations.
The main boundary is cognitive complexity rather than character count. Ruby expressions can contain ternaries, assignments, chained calls, and inline exception handling. Combining those merely to retain endless syntax makes failure paths harder to see and gives a debugger fewer meaningful lines. A standard method body can name intermediate values and put each decision where the reader expects it.
Endless syntax also does not turn a value into a stored attribute. def timestamp = Time.now calls Time.now on every invocation; it is not evaluated once at class definition time. Likewise, a method that returns a mutable object still returns that mutable object. The spelling creates no memoization, purity, or immutability guarantee.
Assignment methods are a special grammar case. A writer such as def name=(value) cannot use the shorthand, even though its implementation might appear to be one assignment. Use the normal form for setters. Operators and ordinary names have their own parsing details, so teams should favour straightforward examples over clever syntax puzzles.
Version compatibility belongs to the decision as well. Endless methods arrived in Ruby 3.0, and early releases described the feature as experimental before it settled into normal use. Application code on a fixed modern runtime can adopt it deliberately. A library supporting older Ruby versions must continue using def ... end, because unsupported syntax prevents the file from loading at all.
Code Example
class Invoice
def initialize(subtotal, tax)
@subtotal = subtotal
@tax = tax
end
def total = @subtotal + @tax
def free? = total.zero?
def summary
label = free? ? 'free' : 'payable'
"#{label}: #{total}"
end
end
puts Invoice.new(100, 18).summary
Common Mistakes & Pitfalls
Using an endless method for multi-statement logic (they must be a single expression), or expecting end.
Check your understanding
Answer these without looking back at the article.
- Does an endless method evaluate its expression once or on every call?
- Which kinds of method become less readable when compressed to one expression?
- Why does a library's supported Ruby version affect this syntax choice?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.