Ruby founder talks about Ruby's blocks and closure Structure

Source: Internet
Author: User
The founder of Ruby talked about the ruby blocks and closure structure at on. I want to repost on Qiu Haifeng's translation network (0) font size: T | T

This conversation is translated from the third part of the interview with Matz on the artima.com website to help you understand the blocks and closure structures in ruby.

AD:

This interview is an interview with Matz, the founder of Ruby on the artima.com website a few years ago. Artima interviews are generally more in-depth at the technical level. If you want to learn more about the features of various languages, artima interviews are worth looking. This article describes Ruby's blocks and closure structures.

Bill Venners:

Ruby supports blocks and closure structures. What are Ruby blocks and closure? How do they use them?

Yukihiro Matsumoto:

Blocks is basically an anonymous function. You may be familiar with Lambda functions in other languages such as LISP or Python. You can pass an anonymous function to another function, which can call the passed anonymous function. For example, a function can execute loop iteration by passing an element to an anonymous function at a time. In programming languages that can regard functions as the first type, this is a common method called high-order function styles. This is true for lisp and python, and even for C, this can be achieved through function pointers. Many other languages can also do this programming.

In Ruby, the difference is that the syntax style of the high-order function is different. In other languages, you must indicate that a function can accept another function as a parameter. However, in ruby, any method can be called as a hidden parameter. In the method, you can use the yield keyword and a value to call the block.

Bill Venners:

What are the benefits of block?

Yukihiro Matsumoto:

Basically, the block is designed for loop iteration abstraction. The most basic use of block is to let you define loop iteration in your own way. For example, if you have a list, sequence, vector group, or array, you can use the method provided in the standard library to implement forward loop iteration, but what if you want to implement loop iteration from the back to the front? If you use the C language, you must first set four things: an index, a starting value, an ending condition, and an incremental variable. This method is not good because it exposes the internal implementation method of the list. We hope to hide the internal logic. By using block, we can hide the internal loop iteration method in a method or function. For example, you can call list. reverse_each to implement a reverse loop iteration on a list without knowing how the list is implemented.

Bill Venners:

That is to say, I pass a block structure. The code in this block can do anything for every element in the loop iteration. As for how to reverse traverse, it depends on the List itself. In other words, I pass the code originally written in the C language loop as a block.

Yukihiro Matsumoto:

Yes, which means you can define many iterations. You can provide a method of forward loop iteration, a method of backward loop iteration, and so on. It depends on you. C # also has an iterator, but it only has one iterator for each class. In Ruby, you can have any number of iterators. For example, if you have a tree class that allows you to traverse data in depth-first or breadth-first mode, you can provide two different traversal methods.

Bill Venners:

Let me think about whether I understand this. in Java, they implement abstract iteration through the iterator interface. For example, the calling program can enable llection to implement iterator. However, the caller must use a loop to traverse the elements returned by the iterator. In a for loop, my code processes the elements of each loop iteration, so that loop statements are always displayed in the calling program. Using block, I do not call a method to obtain an iterator. I just call a method, at the same time, I want to pass the processing code of each element to be processed in the loop iteration as a block structure to this function. The benefit of block is that it extracts some code from the for loop in the calling program.

Yukihiro Matsumoto:

The specific details of loop iteration should be the class that provides this function. The caller should be aware of this as little as possible. This is the original purpose of the block structure. In fact, in earlier versions of Ruby, block methods were called iterators because they were designed to implement loop iteration. But in the Development of Ruby, the use of block has been greatly enhanced later, from the initial loop abstraction to everything.

Bill Venners:

For example?

Yukihiro Matsumoto:

We can create a closure object from the block. A closure object is an anonymous function implemented in lisp. You can pass an anonymous function (closure) to any method to customize the behavior of the method. For example, if you have a sorting method for sorting arrays or lists, you can define a block to define how to compare elements. This is not a loop iteration. This is not a loop, but it uses a block.

Bill Venners:

What makes block a closure?

Yukihiro Matsumoto:

Closure objects contain executable code, including status and execution scope. That is to say, in closure, you can capture the runtime environment, that is, local variables. Therefore, you can reference a local variable in a closure, that is, after the function has been returned, its execution range has been destroyed, and the local variable still exists as a part of the closure object, when no object references it, the garbage collector processes it and the local variable disappears.

Bill Venners:

So, are local variables basically shared by methods and closure objects? If the closure object updates the variable, you can see the method. If the method updates the variable, you can also see the cosure object.

Yukihiro Matsumoto:

Yes, local variables are shared between closure and methods. This is a real closure, and it is not just a copy.

Bill Venners:

What are the benefits of a real closure? Once I change a block into a closure, what can I do with it?

Yukihiro Matsumoto:

You can convert a closure into a block, so closure can be used in any block. Usually, closure is used to save the status of a block in an instance variable, because once you convert a block into a closure, it is an object that can be referenced through a variable. Of course, closure can also be used as in other languages, such as passing to an object to implement the definition of method behavior. If you want to pass some code to define a method, you can certainly pass it a block. but if you want to pass the same code to two methods (this is rare of course), but if you want to do so, you can convert a block into a closure, pass the same closure to multiple methods.

Bill Venners:

In the past, what are the advantages of obtaining the context? What makes Ruby closure really different is that it captures the Context Environment of runtime, local variables, and so on. So what is the benefit of having a context environment that we cannot obtain by passing a code block to an object?

Yukihiro Matsumoto:

Actually, the main reason is to pay tribute to the lisp language. LISP provides a real closure structure, so I hope to continue providing this function.

Bill Venners:

One difference I see is that data is shared between closure objects and methods. I think I can put any required environment data in a regular block with a non-closure structure as a parameter for transmission, but the block is only a copy of the environment data, it's not really closure. it does not share environment data. Sharing is a difference between closure and common traditional function objects.

Yukihiro Matsumoto:

Yes, sharing allows you to do some interesting code demonstration, but I think it is not as useful as imagined in the daily work of programmers. This does not have much to do with it. For example, common replication like Java's internal classes is used in many occasions. But with the clousure structure of Ruby, I want to express my greetings to lisp culture.

The interview is here. I hope you can have a deeper understanding of Ruby's blocks and closure structures.

[Edit recommendations]

  1. Niu Ren commented on the top ten favorite features of the Ruby Language
  2. Python and Ruby: Comparison of popular dynamic scripting languages
  3. Comparison of Ruby and Python syntax
  4. Ruby usage tips: find efficient implementations
  5. Ruby on Rails getting started
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.