The two differences between proc and lambda IN Ruby: proclamloud

Source: Internet
Author: User

The two differences between proc and lambda IN Ruby: proclamloud

1. In proc and lambda, the return keyword has different meanings.:
In proc, return only indicates returning from this lambda.
In lambda, return is not returned from proc, but from the scope that defines proc.

Copy codeThe Code is as follows:
Def one_method
P = Proc. new {return 10} # When this block is called, 10 is directly returned from the scope of p, so the following return will not be executed
Result = p. call
Return result * 2
End
 
Def two_method
P = lambda {return 10} # return 10 from lambda when calling this block
Result = p. call
Return result * 2 # continue execution
End
 
Puts one_method #10
Puts two_method #20

2. In proc and lambda, check parameters in different ways:

In proc, if there are more parameters than defined, extra parameters are ignored. If there are fewer parameters than defined, the parameter that has not been passed is automatically specified as nil.
In lambda, an ArgumentError error is thrown no matter whether the actual parameter is more or less than the defined parameter.

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.