Ruby中的Procs

Ruby Procs (Ruby Procs)

We have gone through many traditional and conventional things in Ruby but procs is a new thing you will come across. We have seen how we can return value inside the Ruby method but we can also store method inside a variable with the help of Procs. This makes our code more flexible and readable. You can store an entire set of instructions or rules inside a variable and can call them anywhere as per the requirement.

我们在Ruby中经历了许多传统的事情,但是procs是您会遇到的新事物。 我们已经看到了如何在Ruby方法中返回值,但也可以在Procs的帮助下将方法存储在变量中。 这使我们的代码更加灵活和可读。 您可以将整个指令或规则集存储在变量内,并可以根据需要在任何地方调用它们。

Syntax of Procs:

Procs的语法:

    variable_name = Proc.new{|argument1,argument2|argument1+argument2}

I think that syntax made you pretty clear about the Procs. Procs can be simply defined with the help of a ".new" method. It requires a block inside which the entire set of the process will be defined. You can specify the argument list inside "||". Now, you will get clearer about it with the help of example which is given below,

我认为语法使您对Procs非常清楚。 可以使用“ .new”方法简单地定义过程 。 它需要一个块,在块中将定义整个过程集。 您可以在“ ||”中指定参数列表 。 现在,借助下面给出的示例,您将更加清楚地了解它,

var = Proc.new{|num1,num2,num3| num1+num2+num3}

p "The sum is #{var[1,3,4]}"

Output

输出量

"The sum is 8"

You can observe in the above code that we have created a proc inside the var variable. The purpose of proc is to calculate the sum of elements entered by the user. These elements are specified inside || and all the processing is done inside the { } block. We are simply calling proc with the help of square brackets and square brackets are having the actual parameters.

您可以在上面的代码中观察到,我们在var变量内创建了一个procproc的目的是计算用户输入的元素总数。 这些元素在||中指定 并且所有处理都在{}块内完成。 我们只是在方括号的帮助下调用proc ,而方括号具有实际参数。

Procs are always created with the help of blocks. Blocks always help us to bind the behavior of the statements. There are two ways to create procs which are stated below,

总是在块的帮助下创建过程 。 块始终可以帮助我们绑定语句的行为。 下面介绍了两种创建proc的方法:

1) With the help of curly braces

1)在花括号的帮助下

We have seen this in the above example where we are creating the proc with the help of curly braces. For your better reference, one more snippet is given below,

在上面的示例中,我们已经在花括号的帮助下创建了proc。 为了给您更好的参考,下面再提供一个摘要,

var = Proc.new{
	|num|
	i = 1 

	while(i<=10)
		p num * i
		i = i + 1
	end
}
var[12]

Output

输出量

12
24
36
48
60
72
84
96
108
120

2) With the help of do end block

2)借助do端块

procs are also be created with the help of do.. end block. This way makes the code more understandable. Let us understand this with the help of an example. Let us modify the above example in the following way,

proc也可以在do .. end block的帮助下创建。 这种方式使代码更易于理解。 让我们借助示例了解这一点。 让我们以以下方式修改上面的示例,

var = Proc.new do |num|
	i = 1 
	while(i<=10)
		p num * i
		i = i + 1
	end
end
var[17] 

Output

输出量

17
34
51
68
85
102
119
136
153
170

You can call the procs in two ways: one by just writing the name of the variable and the other one is by using the call method. You can use the call method in the following way,

您可以通过两种方式调用proc :一种是仅编写变量名称,另一种是使用call方法 。 您可以通过以下方式使用call方法

var = Proc.new {|a| a*5}
var.call(12)

Both methods will give you the same results. It is up to you which one you like most.

两种方法都会给您相同的结果。 您最喜欢哪一个取决于您。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值