Using block, Proc, and lambda to implement closure _ruby topics in Ruby

Source: Internet
Author: User
Tags closure

Closures (Closure) are free code that is not bound to any object, and the code in the closure is independent of any objects and global variables, and is only relevant to the context in which the code is executed.

Today we'll take a quick look at the closure implementations in Ruby.

The closure implementations in Ruby are: Block,proc,lambada.

First, let's look at block.

Copy Code code as follows:

ary = [1,2,3,4]

ary.collect! Do |a|

A*a

End

Ary.each do |a|

Puts a

End

In this code, we use the Block method of the array object to square each element in the ARY. From the example we can see that block is easy to use, presumably traditional Java and C code, omitting redundant loops, allowing you to focus more on business code, which is one of the benefits of Ruby.

The great advantage of using block is that you can omit redundant code that repeats redundancy, and we'll look at an example of a read file:

Copy Code code as follows:

#file Block

File.Open (__file__) do |f|

Puts F.readlines

End

When we read files in Java code, we resent that lengthy try-catch-finally. From the ruby code above, I can see that the Ruby language gives you a handle, and you don't have to write useless system code through block.

As we can see from the example above, the block in Ruby does not need to write redundant system code, but rather focus on business code and improve development efficiency.

Second, we look at the proc.

If you use block a lot, you'll find a problem: There will be lots of duplicate blocks in the code, like a lot of places that need to print the contents of a file. How to solve it? The first thing you think about is writing a public function, yes, it can be solved. But remember that you are using the Ruby language and don't reinvent the wheel. Ruby provides a functional block:proc.

Let's look at a simple proc example:

Copy Code code as follows:

#file block with Proc

p = proc.new{|f| puts F.readlines}

File.Open (__file__, &p)

As you can see in the example above, the block code is defined as a Proc object, and then replaced with proc where block is used, thus achieving perfect code reuse.

Finally, let's look at the lambda.

A lambda is the name of an expression that can be created in Ruby by a lambda expression proc object. Let's look at the examples first:

Copy Code code as follows:

#file block and Proc created with lambda

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

File.Open (__file__, &new_p)

In the example above, we created a proc object using the lambda method of the system, and the effect is the same as what Proc.new created. In fact, using a lambda is the same as using a proc.new effect, except that the lambda is a standard method, and other languages like Python support it, so Ruby also supports it.

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.