Two distinctions between proc and Lambda in Ruby _ruby topics

Source: Internet
Author: User

1. In proc and lambda, the return keyword has different meanings :
In proc, return only means returning from this lambda.
In a lambda, return is not returned from the proc, but rather from the scope in which the proc is defined.

Copy Code code as follows:

def One_method
p = Proc.new{return #调用这个块的时候, returns 10 directly from the scope of P, so the following return will not execute
result = P.call
return result * 2
End

def Two_method
p = Lambda{return #调用这个块的时候, returning 10 from the lambda
result = P.call
return result * 2 #继续执行
End

Puts One_method # 10
Puts Two_method # 20

2, in proc and lambda, check the parameters in different ways :

In proc, if the parameter is more than defined, the redundant parameter is ignored, and if the parameter is less than defined, the parameter not passed over is automatically specified as nil.
In a lambda, a argumenterror error is thrown regardless of whether the actual argument 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.