Skip to content

Ruby Keyword: return

☈king edited this page Nov 19, 2012 · 3 revisions

Leave the current method, making the given value.

Keep in mind that the last statement in a Ruby method is its implied value, so these are equivalent:

def f x
  if 0 == x % 3
    return true
  else
    return false
  end
end

def f x
  if 0 == x % 3
    true
  else
    false
  end
end

…but, then, you might as well go all the way with it, as this is equivalent:

def f x
  0 == x % 3
end

So, usually, it's only useful as an "early return" in Ruby, such as:

return if arg > 3
# rest of method here

Note that you can return out of lambdas but not proc's, e.g.:

-> { return 3 }.()
# ⇒ 3
proc { return 3 }.()
# ⇒ LocalJumpError: unexpected return
Clone this wiki locally