当前位置: 首页 > 工具软件 > procs > 使用案例 >

ruby lambda_Ruby中Lambda和Procs之间的区别

公良运锋
2023-12-01

ruby lambda

We know how to declare a lambda and proc in Ruby? Their implementation is almost the same and they both are used for the same purpose. For a quick revision, Procs and Lambdas are used to store methods or set of logics inside a variable. You can call that variable inside your code wherever you want. But there exist some differences between them. Let us see how they differ from each other.

我们知道如何在Ruby中声明lambdaproc吗? 它们的实现几乎相同,并且都用于相同的目的。 为了进行快速修订,Procs和Lambdas用于在变量内存储方法或逻辑集。 您可以在代码中的任意位置调用该变量。 但是它们之间存在一些差异。 让我们看看它们之间的区别。

Declaration:

宣言:

Procs are required to be declared with the help of ".new" method whereas you can declare a lambda just by writing the keyword "lambda". Refer the example given below and analyze the difference,

特效都需要用“新”方法的帮助下被宣布,而你可以宣布一个拉姆达只是写关键字‘拉姆达’。 请参考以下示例并分析差异,

prc = Proc.new{|ele| puts ele*10}
lmbda = lambda {|ele| puts ele*10}
prc.call(12)
lmbda.call(34)

Output

输出量

120
340

参数计数 (Argument count)

They both differ in the behavior of argument count as well. Procs do not count arguments whereas lambda does which means that if you pass the wrong number of arguments inside the call method of lambda then you will have to face an error whereas if you do the same in case of procs you will not face any kind of Exceptional situation. Refer the example given below and analyze the code,

它们在参数计数的行为上也不同。 Procs不计算参数,而lambda进行计数,这意味着如果在lambda的调用方法中传递了错误数量的参数,那么您将不得不面对错误,而如果在procs中执行相同的操作,则将不会遇到任何错误。特殊情况。 请参考下面给出的示例并分析代码,

prc = Proc.new do |ele,ele1,ele2,ele3| 
	puts ele+ele1+ele2*ele3
end

lmbda = lambda do|ele,ele1,ele2,ele3|
    puts ele*10+ele2+ele1+ele3
end

prc.call(12,11,22,23,66,77)
lmbda.call(34,34,2,4)

Output

输出量

529
380

Explanation:

说明:

In the above code you can observe that in the call method of proc we are passing the wrong number of arguments or you can say that the number of arguments is more than expected by the proc but still interpreter is not giving any kind of error but if we do the same in the case of lambda we will have to face an error named as "the wrong number of arguments (given 5, expected 4): Argument error".

在上面的代码中,您可以观察到在proc的调用方法中,我们传递了错误的参数数量,或者您可以说参数数量超出了proc的预期,但解释器没有给出任何错误,但是如果对于lambda,我们执行相同的操作,我们将不得不面对一个名为“错误的参数数量(给定5,预期为4):Argument error”的错误

退货行为 (Return behavior)

This is the major difference you will find between procs and lambdas. Procs behave in a way that it will get explicitly out from the code when it will find any return statement or you can say that the further processing will be stopped once it finds the return keyword whereas this is not the case of lambdas it first completes all the processing then it gets out from the block. Refer to the code given below and analyze the difference.

这是proc和lambda之间的主要区别。 Procs的行为方式是,它将在找到任何return语句时从代码中明确退出,或者可以说,一旦找到return关键字,将停止进一步处理,而对于lambda则不是这种情况,它首先完成所有操作处理然后从块中退出。 请参考下面给出的代码并分析差异。

def method1
  x = lambda { return }
  x.call
  p "Welcome to Includehelp.com from lambda"
end

def method2
  x = Proc.new { return }
  x.call
  p "Welcome to Includehelp.com from Procs"
end

method1
method2

Output

输出量

"Welcome to Includehelp.com from lambda"

In the output, you can observe that the string from lambda is getting printed only because proc has found the return statement and it got exit leaving behind the unprocessed statements.

在输出中,您可以看到仅由于proc找到了return语句而退出了lambda的字符串,并且退出了未处理的语句,所以退出了该字符串。

翻译自: https://www.includehelp.com/ruby/lambdas-vs-procs.aspx

ruby lambda

 类似资料: