You Don't Need to Return Explicitly

October 21, 2009

In Ruby, every method returns the value of the last expression evaluated. There is no need to spell it out.

Don't do this:

def foo
  value = Foo.first(:conditions => { :label => "bar" })
  return value
end

Do this instead:

def foo
  Foo.first(:conditions => { :label => "bar" })
end

The return keyword still has its place — early returns for guard clauses, for instance — but if you are just returning the last expression, let Ruby do what Ruby does.

Questions or thoughts? Get in touch.