For details about return, break, and next in Ruby, rubyreturn

Source: Internet
Author: User

For details about return, break, and next in Ruby, rubyreturn

The use of the return, break, and next keywords involves the issue of jumping out of the scope. Their difference lies in the different scopes of different keywords jumping out, because there are code blocks, you need to pay special attention to them.

Return

Common Methods

Generally, the return statement is the same as what you understand.
Copy codeThe Code is as follows:
Def m1 param
If param = 1
Return 'returned 1'
End
'Returned default value' # according to the Ruby language specification, the result of the last statement will be returned as the return value. retu rn is optional.
End

M1 (1) # => returned 1
M1 (2) # => returned default value

When there is an ensure with exception capture, the situation will be slightly different:
Copy codeThe Code is as follows:
Def m1
'Return default'
Ensure
Puts 'I am sure that it will be here! '
End

M1 # => return default

In this case, the m1 method returns the value before ensure no matter whether return is displayed or not before the ensure statement, the ensure statement only ensures that the subsequent code block puts 'I am sure that it will be here! . If the return value is used in the ensure statement, the situation is different. Example:
Copy codeThe Code is as follows:
Def m1
Return 'Return default'
Ensure
Return 'I am sure that it will be here! '
End

M1 # => I am sure that it will be here!

Whether or not the returned value is displayed before ensure, only the value after ensure is returned.

In case of code block intervention, it will be different:
Copy codeThe Code is as follows:
Def m1
P 'start ...'
Proc do
P'block start'
Return
P'block end'
End. call
P 'end ...'
End

M1

# Output result:
#
# "Start ..."
# "Block start"

This should be expected. Let's look at the next one:
Copy codeThe Code is as follows:
Def m1
P 'start ...'
-> Do
P'block start'
Return
P'block end'
End. call
P 'end ...'
End

M1

# Output result:
#
# "Start ..."
# "Block start"
# "End ..."

Here there is a line of "end...". Why? This is the biggest difference between Proc and Lambda. Among them, the return Statement jumps out of different scopes. Proc directly jumps out of the call of the entire method, while Lambda only jumps out of its own scope, return to the method to continue execution, which requires special attention. (In break, the bounce methods of Proc and Lambda are the same as the return method, and I will not go into details later .)

Break

Let's look at a simple example:
Copy codeThe Code is as follows:
Result = [1, 2, 3, 4, 5]. map do | I |
I * 2
End

P result # => [2, 4, 6, 8, 10]

This is nothing strange. Let's take a look at the following to guess what the output result is?
Copy codeThe Code is 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 is executed, it jumps directly to the FLAG, that is, the map method is jumped out, and the statements in the map method are not completed, resulting in no return value, to verify that this idea is correct, we can use the break of the Ruby language to verify the features with the returned value:
Copy codeThe Code is 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 guess is correct. Although this problem is illustrated above, it should not be very easy to understand. Let's define a code block and explain it again:
Copy codeThe Code is as follows:
Def m1
P'start in m1 ...'
M2 do # code block
P 'start in block in m1 ...'
P 'end in block 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 block in m1 ..."
# "End in block in m1 ..."
# "End in m2 ..."
# "End in m1 ..."

Then we add a break to the block in m1 to see the execution result:
Copy codeThe Code is as follows:
Def m1
P'start in m1 ...'
M2 do # code block
P 'start in block in m1 ...'
Break
P 'end in block 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 block in m1 ..."
# "End in m1 ..."

We can see that the code in the last line of the code block is not executed, and the last line of m2 is not executed. That is, because this line is not executed, the map in the second example of break does not return any value. To sum up, the break in the code block directly jumps out of the calling method (m2), and continues to execute the remaining statements in this method (m1) in the method (m1) that declares the code block.

Next

The next keyword is similar to the continue in other languages. It works basically in the same way as the continue.
Copy codeThe Code is as follows:
Def m1
P'start in m1 ...'
M2 do # code block
P 'start in block in m1 ...'
Next
P 'end in block 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 block in m1 ..."
# "End in m2 ..."
# "End in m1 ..."

Only the last line of code in the code block is skipped. This is how next works. Let's take a look at the break example. If we use next to write, what is the result? If you fully understand the above, I believe you can calculate the result in the brain:
Copy codeThe Code is 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 return values: ''' result = [1, 2, 3, 4, 5]. map do | I | next 'Next' if I> 3 I * 2 end
Copy codeThe Code is as follows:
P result # => [2, 4, 6, "next", "next"] '''

Others

Return can be used in methods and code blocks, while break and next can only be used in code blocks (loop structures can also be used, but it is usually expressed in the form of a code block). If you call the two methods, a syntax error is prompted, that is:
Copy codeThe Code is as follows:
Def m1
Return # OK
Break # Invalid break, compile error (SyntaxError)
Next # Invalid next, compile error (SyntaxError)
End

Conclusion

Return is similar to other languages in most cases. Pay attention to the details in two different code blocks: ensure and Proc and Lambda.

Note that break is always returned to the method that calls the code block method in code blocks with nested methods ).

Next is the most honest, and you do not need to pay attention to it.

Finally, not only can return values, but both break and next can return values.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.