ruby lambda_Ruby中的Lambda

ruby lambda

RubyLambdas (Ruby Lambdas)

In the last tutorial, we have seen what are procs, how they are implemented in Ruby and their examples? Lambdas are pretty similar to Procs. They are also used to store a set of instructions or a method inside a variable which is also the feature of Procs. You can call lambdas anywhere in your code wherever required.

在上一教程中,我们了解了什么是proc ,如何在Ruby中实现它们以及它们的示例? Lambda与Procs非常相似。 它们还用于在变量内存储一组指令或方法,这也是Procs的功能。 您可以在需要的任何地方在代码中的任何地方调用lambda

Let us see the basic syntax of Lambdas in Ruby for understanding them in a better way,

让我们看看Ruby中Lambda基本语法,以便更好地理解它们,

    variable_name = lambda {|argument1,argument2| argument1 * argument2}

You can observe that the syntax is quite similar to Procs but in Proc, we need to use ".new" method to create a Proc but here writing lambda is enough for declaring a lambda. In the above syntax, we are using curly braces and inside them, we are passing the arguments inside || block or pair of alteration symbol. We have demonstrated by multiplying the argument whereas you can do any type of processing inside the block.

您可以观察到语法与Procs非常相似,但是在Proc中,我们需要使用“ .new”方法来创建Proc,但是在这里编写lambda足以声明lambda 。 在上面的语法中,我们使用花括号,在花括号中,我们在||内部传递参数。 块或成对的更改符号。 我们通过乘以参数进行了演示,而您可以在块内进行任何类型的处理。

Now, let us go through an example of Lambda, so that we will be able to see its implementation in Ruby code.

现在,让我们看一下Lambda示例 ,以便我们能够在Ruby代码中看到其实现。

var = lambda {|arg1,arg2,arg3| puts arg1*arg2+arg3}
var[12,45,33]

Output

输出量

573

In the above code, we are creating lambda which is used to process some kind of arithmetic calculations. We are calling it by writing its name and passing arguments inside square braces like [].

在上面的代码中,我们正在创建用于处理某种算术计算的lambda 。 我们通过写它的名称并在[[]之类的方括号内传递参数来调用它。

You can also create a lambda in the following way. It is known as stabby lambda. The example is given below,

您还可以通过以下方式创建lambda 。 它被称为稳定的λ。 下面是示例

var = -> (arg1,arg2,arg3) {puts arg1*arg2+arg3}
var[12,45,33]

Output

输出量

573

We have created a stabby lambda here. The above code is modified to create a better understanding of differences that come in terms of syntax. Here we are passing arguments inside () braces and the processing is done inside the curly braces { }.

我们在这里创建了一个稳定的lambda 。 修改以上代码以更好地理解语法方面的差异。 在这里,我们在()括号内传递参数,并且在花括号{}内完成处理。

调用lambda (Calling a lambda)

We have seen this in Procs as well. Lambdas are called in the same way as Procs. There are two ways, first one is just by writing the name of the variable along with the arguments inside the square braces and the second one is by the help of the call method. The syntax is given below,

我们也在Procs中看到了这一点。 Lambda与Procs的调用方式相同。 有两种方法,第一种是通过将变量的名称以及方括号内的参数一起编写,第二种是通过call方法的帮助。 语法如下:

    variable_name.call( argument1, argument2)

Let us see its practical implementation with the help of example given below,

让我们借助以下示例查看其实际实现,

var = lambda{
	|num|
	i = 1 
	while(i<=10)
		p num * i
		i = i + 1
	end
}
var.call(34)

Output

输出量

34
68
102
136
170
204
238
272
306
340

In the above code, we are using lambda.call() method for calling the lambda.

在上面的代码中,我们使用lambda.call()方法来调用lambda。

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

ruby lambda

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值