Two differences between proc and Lambda in Ruby

Source: Internet
Author: User

This article mainly introduces the two differences between Proc and Lambda in Ruby, this article explains that in proc and lambda, the return keyword has different meanings, check the parameters of the different ways two important differences, need friends can refer to the

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.

The code is as follows:

def One_method

p = Proc.new{return # When calling this block, 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 # when calling this block, returning 10 from the lambda

Result = P.call

Return result * 2 #Continue execution


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.