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.
Copy Code code as follows:
def M1 param
if param = 1
Return ' returned 1 '
End
' returned default value ' #根据Ruby语言规范, the result of the last execution statement will be returned as the return value, Retu rn is optional
End
M1 (1) # => returned 1
M1 (2) # => returned default value
The situation is slightly different when there is an exception capture ensure:
Copy Code code as follows:
DEF M1
' Return default '
Ensure
Puts ' I am sure that 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:
Copy Code code 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:
Copy Code code as follows:
DEF M1
P ' Start ... '
Proc Do
P ' block start '
Return
P ' Block end '
End.call
P ' End ... '
End
M1
# Output Results:
#
# "Start ... "
# "Block Start"
This should be expected, and then look at the next:
Copy Code code as follows:
DEF M1
P ' Start ... '
-> do
P ' block start '
Return
P ' Block end '
End.call
P ' End ... '
End
M1
# Output Results:
#
# "Start ... "
# "Block Start"
# "End ... "
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:
Copy Code code as follows:
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?
Copy Code code as follows:
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:
Copy Code code as follows:
result = [1, 2, 3, 4, 5].map do |i|
Break ' returned break ' if I > 3
I * 2
End
P Result # => ' returned break '
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:
Copy Code code as follows:
DEF M1
P ' Start in M1 ... '
M2 Do # code block
P ' start in blocks 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 Results:
#
# "Start in M1 ... "
# "Start in m2 ... "
# "Start in blocks in M1 ... "
# "End in blocks in M1 ... "
# "End in M2 ... "
# "End in M1 ... "
Then we add a break to the block in the M1 to see the execution result:
Copy Code code as follows:
DEF M1
P ' Start in M1 ... '
M2 Do # code block
P ' start in blocks in M1 ... '
Break
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 Results:
#
# "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.
Copy Code code as follows:
DEF M1
P ' Start in M1 ... '
M2 Do # code block
P ' start in blocks 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 Results:
#
# "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:
Copy Code code as follows:
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
Copy Code code as follows:
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:
Copy Code code as follows:
DEF M1
Return # OK
Break # Invalid Break, compile error (SYNTAXERROR)
Next # Invalid Next, compile error (SYNTAXERROR)
End
Conclusions
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.