Implementing closures using Block, Proc, and lambda IN Ruby

Source: Internet
Author: User

Closure refers to the free code that is not bound to any object. The code in the Closure is irrelevant to any object and global variables and is only related to the context of the code execution.

Today, let's take a brief look at the closure implementation in ruby.

The closure implementations in Ruby include: Block, Proc, and Lambada.

First, let's look at Block.
Copy codeThe Code is as follows:
Ary = [1, 2, 4]

Ary. collect! Do | a |

A *

End

Ary. each do | a |

Puts

End

In this Code, we use the block Method of the Array object to square each element in ary. From the example, we can see that the block is very convenient to use. Presumably, the traditional Java and C encoding will omit the redundant loop, so that you can focus more on Business Code, this is also one of the benefits of ruby.

The biggest benefit of using block is that you can omit redundant and useless code. Let's look at an example of reading a file:

Copy codeThe Code is as follows:
# File block

File. open (_ FILE _) do | f |

Puts f. readlines

End

Compared to reading files using Java code, every time we dislike the lengthy try-catch-finally. From the ruby code above, we can see that the ruby language has done something for you. With block, you don't need to write useless system code.

From the above example, we can see that for developers, the Block in Ruby does not need to write redundant and useless system code, but focuses more on Business Code and improves development efficiency.

Next, let's look at Proc again.

If you use Block frequently, you will find a problem: there are many duplicate blocks in the Code. For example, you need to print the file content in many places. How can this problem be solved? The first thing you think of is to write a public function, which is good and can be solved. But remember that you are using the ruby language and do not recreate the wheel. Ruby provides a functional Block: Proc.

Let's look at a simple Proc example:
Copy codeThe Code is as follows:
# File block with Proc

P = Proc. new {| f | puts f. readlines}

File. open (_ FILE __, & p)

The above example shows that the Block code is defined as a Proc object, and then replaced with Proc where the Block is used, thus achieving perfect code reuse.

Finally, let's look at lambda.

Lambda is the name of an expression. In ruby, you can use a lambda expression to create a Proc object. Let's first look at the example:
Copy codeThe Code is as follows:
# File block with Proc created with lambda

New_p = lambda {| f | puts f. readlines}

File. open (_ FILE __, & new_p)

In the above example, a Proc object is created using the system lambda Method, and the effect is the same as that created by Proc. new. Actually, using lambda has the same effect as using Proc. new, but lambda is a standard method. Other languages such as Python also support it, so it is also supported in ruby.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.