208 block, proc and lambda in Ruby [Rails backend development training camp]

1 what is Block

Ruby's concept of block is also called closure in other languages. In essence, a block is the same as a method, except that it has no name and does not belong to an object. A block is a piece of code that accepts parameters and returns values and is always called by passing it to a method. The expressions are block, proc and lambda. Proc is the object-oriented encapsulation of block, and lambda is the further encapsulation of proc.

Let's meet block first.

There are two expressions for blocks. One is do... end, which contains multiple lines of code, and the other is {}, which contains only one line of code.
In ruby, we can use each when we loop through an array. Blocks are often combined with hashes and arrays to form Iterators. Here we give an example of block reception parameters.

[1, 2, 3].each { |item| p item }

You can also use the following form:

[1, 2, 3].each do |item|
  p item
end

The part contained in braces or the part contained in do... end is a block.

Initially, we can pass a piece of code to the method and execute it in the method.

We can also define a function by ourselves, and then pass the block parameter when we call this function.

Examples are as follows

def func1
  puts "hello"
  yield
end

func1 { puts "world" }

The running results of the above examples are:

hello
world

Usually, the last parameter of a function can receive a block. We don't need to add a block parameter to the function parameter list, but we can pass it directly.

2 Proc

Proc is an object-oriented encapsulation of block. We can create a block object using the proc display. Then send the call event to this object to call and execute the code fragment in proc.

Examples are as follows

rectangle_area = Proc.new { | a, b | puts a * b }
rectangle_area.call( 5, 6 )
def rectangle_area_with_length (length)
  Proc.new{ | width | width * length }
end

area = rectangle_area_with_length(6)
area.call(3)
area[3]
area.class # => Proc

3 lambda

lambda is a Ruby function used to create Proc.

multiply_lambda_proc = lambda { | x, y | x * y }
multiply_lambda_proc.call( 3, 4 ) # return 12

4. Difference between proc and lambda

Although both Proc class and lambda can create blocks. However, the block created by lambda checks the parameters, while Proc does not.

multiply_lambda_proc = lambda { | x, y | x * y }
multiply_proc = Proc.new { | x, y | x * y }

multiply_lambda_proc.call( 3, 4, 5 ) # ArgumentError: wrong number of arguments (3 for 2)
multiply_proc.call( 3, 4, 5 ) # return 12 as normal
multiply_proc.call( 3 )  # TypeError: nil can't be coerced into Fixnum

Lambda is essentially a function, so it will be returned from the lambda function when it encounters return,

Proc is a piece of executable code, which is equivalent to inserting a broken code into the function. When the return statement is included in proc, the whole function will return

5. How to convert a block into a proc object?

  1. Proc.new + code block
Proc.new { |imsi| where(imsi: imsi) }
  1. Proc + code block (proc abbreviation)
proc { |imsi| where(imsi: imsi) }
  1. lambda + code block
lambda{ |rs| where.not(report_status: rs) }
  1. ->+ code block (lambda abbreviation)
-> (rs) { where.not(report_status: rs) }

6 Summary

Block is a very powerful function, which enables us to write very flexible code, has high readability, and can be used everywhere. Lambda is like a function. The return value of return is itself. Lambda can be understood as a special Proc instance. The object created by Proc.new does not check the number of parameters, and the other two methods (proc | lambda) check the number of parameters.

Keywords: Python Ruby Algorithm

Added by st0rmer on Thu, 25 Nov 2021 01:50:13 +0200