What if your method returns something unexpected just because you forgot to say return?
Why Explicit return statement in Ruby? - Purpose & Use Cases
Imagine you write a method in Ruby that does some calculation, but you forget to clearly say what value it should give back. You just write the steps, hoping Ruby will guess right.
This can cause confusion because Ruby might return the last line automatically, but if you want to stop early or return something specific, it's easy to make mistakes or misunderstand what the method actually returns.
Using an explicit return statement lets you clearly say, "This is the value I want to send back right here." It makes your code easier to read and avoids surprises.
def add(a, b)
a + b
enddef add(a, b) return a + b end
It enables you to control exactly what your method sends back, making your code clearer and less error-prone.
When checking if a user is allowed access, you might want to return false immediately if a condition fails, instead of running the whole method.
Without explicit return, Ruby returns the last evaluated expression.
Explicit return makes your intentions clear and code easier to understand.
It helps avoid bugs by controlling exactly when and what value is returned.