Return,break,next the use of these keywords involves jumping out of scope, and their difference lies in the scope of the different keywords jumping out, because there are blocks of code that cause some places to pay extra attention.
Return
Common Ways
Usually the return statement is the same as the one you understand.
def M1 param
if param = 1
return ' returned 1 '
end ' returned default value ' # According to the Ruby language specification, the result of the last execution statement will be returned as Value returned, return is optional end
M1 (1) # => returned 1
M1 (2) # => returned default value
The situation is slightly different when there is an exception capture ensure:
def M1
' return default '
ensure
puts ' I am sure ' It'll be here! '
End
M1 # => return default
In this case, before the ensure statement, whether or not a return is displayed, the M1 method returns the value before ensure, and the ensure statement is just a guaranteed code block puts ' I am sure ' it will be here! ' execution, but will not return from here. The situation is different if you return a value in the ensure statement by returning it. Examples are as follows:
DEF M1 return "return
default"
ensure return
' I am sure ' It'll be here! '
End
M1 # => I am sure that it would be here!
Whether or not a return is displayed before ensure, only the value after ensure is returned.
In the case of code block intervention, it will be different:
def M1
P ' Start ... '
proc do
p ' blocks start ' return p ' blocks end
'
end.call
p ' End ... '
end M1
# output result:
# #
' Start ... "
#" block Start "
This should be expected, and then look at the next:
def M1
P ' Start ... '
-> do
p ' blocks start ' return p ' blocks end
'
end.call
p ' End ... '
end M1
# output result:
# #
' Start ... "
#" block start "
#" ... "
Here's one more line "end ... "And why?" This is the biggest difference between the proc and the Lambda, where the return statement jumps out of the scope of the destination, and the proc jumps out of the entire method, and the lambda jumps out of its scope and returns to the method to continue, which requires extra attention. (In a break, the proc and Lambda jump out the same way and return is the same, after no longer repeat.) )
Break
Let's look at a simple little example:
result = [1, 2, 3, 4, 5].map do |i|
I * 2
end
p result # => [2, 4, 6, 8, 10]
This is no surprise, so take a look at this and guess what the output is?
result = [1, 2, 3, 4, 5].map do |i|
Break if i > 3
i * 2
end
# FLAG
p Result
is [1, 2, 3, nil, nil]? or [1, 2, 3]? Or what? The answer is nil, because after the break, jump directly to the flag, that is, out of the map method, and the statement in the map method is not executed, resulting in no return value, in order to verify that the idea is correct, we You can verify that the Ruby language break can be validated with the attribute of the return value:
result = [1, 2, 3, 4, 5].map do |i|
Break ' returned break ' if I > 3
i * 2
end
p result # => "returned"
Here we can prove that our guesses are correct. Although the above illustrates this problem, but it should not be very easy to understand, we define a block of code, and then to explain:
def M1
P ' start in M1 ... '
m2 do # code blocks
P ' start in M1 ... ' P ' end in blocks in
M1 ... "
end
P" end in M1 ... '
end
def m2 &block
p ' Start in m2 ... '
block.call
p ' End in M2 ... '
end M1
# output result:
#
# ' start in M1 ... "
#" start in m2 ... "
#" start in blocks in M1 ... "#" End in block in
M1 ... "
#" End in M2 ... "
#" end in M1 ... "
Then we add a break to the block in the M1 to see the execution result:
def M1
P ' start in M1 ... '
m2 do # code blocks
P ' start in M1 ... "
break
P" "End" in M1 ... "
end
P" end in M1 ... '
end
def m2 &block
p ' Start in m2 ... '
block.call
p ' End in M2 ... '
end M1
# output result:
#
# ' start in M1 ... "
#" start in m2 ... "
#" start in blocks in M1 ... "
#" end in M1 ... "
You can see that the last line of code block does not execute, and the last line of M2 is not executed because the line is not executed, and the map in the second example of the break does not return any values. To sum up, a break in a code block jumps directly out of the calling method (M2) and continues to execute the remaining statements in this method (M1) in the method that declares the code block (M1).
Next
The next keyword resembles the continue in other languages, and it works in a similar way to continue.
def M1
P ' start in M1 ... '
m2 do # code blocks
P ' start in M1 ... '
next p ' end in blocks in
M1 ... "
end
P" end in M1 ... '
end
def m2 &block
p ' Start in m2 ... '
block.call
p ' End in M2 ... '
end M1
# output result:
#
# ' start in M1 ... "
#" start in m2 ... "
#" start in blocks in M1 ... "
#" End in M2 ... "
#" end in M1 ... "
Just skip over the last line of code block, and that's how next works. Let's take another look at the break example. If you write with Next, what is the result? If you fully understand what is written above, believe that you have been able to figure out the results in your brain:
result = [1, 2, 3, 4, 5].map do |i|
Next if i > 3
i * 2
end
p result # => [2, 4, 6, nil, nil]
The next statement can also bring the return value:
result = [1, 2, 3, 4, 5].map do |i|
Next ' next ' If I > 3
i * 2
end
p result # => [2, 4, 6, ' next ', ' next ']
Other
For return, which can be used in a code block in a method, and break and next can only be used in a block of code (which is also available in a loop structure, but generally it is also represented in the form of a block of code), and if you call both in the method prompts for a syntax error:
def M1 return
# OK break
# Invalid break, compile error (SyntaxError)
next # Invalid next, compile error (SyntaxError)
End
Conclusion
Return most of the cases are the same as other languages and need to be aware of the details in the ensure and proc and lambda two different blocks of code.
A break needs to be noted in a code block in a method nested call, and it always returns to the method that calls the code block method (a bit around).
Next most honest, basically do not need to pay attention to anything.
Finally, it's not just return that returns the value, both the break and next can return a value.