Proc class and proc class method in Ruby Usage analysis of new

Time:2021-12-15

ProcIt is the process object obtained after objectifying the block and its context (scope of local variables and stack framework). You can use proc like an anonymous function, but it does not import the scope of local variables (dynamic local variables can be used as proc local variables).

In the following example, the VaR variable can only be called because proc always maintains the scope of the local variable.

?
1
2
3
4
5
6
7
8
9
var = 1
$foo = Proc.new { var }
var = 2
 
def foo
 $foo.call
end
 
p foo    # => 2

After returning from the method that generates the proc, if return or return occurs in the proc, the localjumperror exception will be thrown.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
def foo
 proc { return }
end
 
foo.call
# => in `call': return from proc-closure (LocalJumpError)
 
def foo
 proc { retry }
end
 
foo.call
# => in `call': retry from proc-closure (LocalJumpError)

If you prefix proc with “&” and pass it to a method with blocks, it works like calling blocks. But strictly speaking, there are still the following differences.

?
1
2
3
4
5
6
7
8
9
10
11
12
#No problem
(1..5).each { break }
 
#In Ruby 1.6 7. No problem in 1.8. In 1.6 An exception occurs in 8
proc = Proc.new { break }
(1..5).each(&proc)
 
#In Ruby 1.6, it is localjumperror
#In Ruby 1.8, run each again
proc = Proc.new { retry }
(1..5).each(&proc)
#=> retry from proc-closure (LocalJumpError)

This is the limitation when the proc object is used as a call block.

?
1
2
Proc.new
Proc.new { ... }

After objectifying the block and its context, the result is returned.

If no block is given, the block carried by the method calling the method is converted into a proc object and returned.

?
1
2
3
4
5
6
def foo
  pr = Proc.new
  pr.call(1,2,3)
end
foo {|args| p args }
# => [1, 2, 3]

 

Proc. New method depth
Proc. New returns the result after objectifying the block and its context.

If no block is given, the block carried by the method calling the method is converted into a proc object and returned.

?
1
2
3
4
5
6
7
8
9
10
11
12
def foo
  pr = Proc.new
  pr.call(1,2,3)
end
foo {|args| p args }
# => [1, 2, 3]
This is the same as the following example
def foo
 yield(1,2,3)
end
foo {|args| p args }
# => [1, 2, 3]

If the calling method does not have a block, an argumenterror exception is thrown.

?
1
2
3
4
5
6
7
def foo
 Proc.new
end
foo
# => -:2:in `new': tried to create Proc object without a block (ArgumentError)
     from -:2:in `foo'
     from -:4

When using proc New, if the proc#initialize method is defined, it will be called when the object is initialized. In addition, it is the same as proc.

Using proc The new method, or specifying a block for the proc method, can create a proc object representing the block.

Execute the block by calling the proc#call method. When calling the proc#call method, the parameters are used as block variables, and the value of the last expression in the block is the return value of proc#call. Proc #call also has a name called proc #[].

?
1
2
3
4
5
6
7
8
#Processing for judging whether the year of the Western calendar is a leap year
leap = Proc.new do |year|
 year % 4 == 0 && year % 100 != 0 || year % 400 ==0
end
 
p leap.call(2000#=> true
p leap[2013]     #=> false
p leap[2016]     #=> true

After setting the block variable to the form of * array, you can receive a variable number of parameters in the form of array like method parameters.

?
1
2
3
4
5
6
double = Proc.new do |*args|
 args.map{|i| i * 2 #Double all elements
end
 
p double.call(1, 2, 3#=> [2, 3, 4]
p double[2, 3, 4]     #=> [4, 6, 8]

In addition, almost all parameter forms that can be used when defining common methods, such as default parameters and keyword parameters, can be used to define block variables and assigned to proc#call methods.