A return statement is not illegal, however it returns from the method call where it was defined... if that method call is still valid.
So this works:
def valid_method_call
pp = Proc.new { return :to_method_caller }
pp.call
end
valid_method_call
But this does not:
ll = lambda {
pp = Proc.new { return :to_lambda_caller }
pp.call
}
ll.call
If you do something like save the proc for later, you are in trouble:
def temporary_method_call
Proc.new { return :where_shall_i_return? }
end
pp = temporary_method_call
pp.call
Because the method call where the proc was defined no longer exists. So Ruby closes over local variable bindings, saving them for later, but not over control flow.
If it did, the return keyword would end up acting like a continuation.