Simple Ruby iterator

Source: Internet
Author: User
Due to work relationships, I have never had time to learn Ruby. Recently, I finally had time to learn Ruby. I also opened a blog on csdn to record my learning experience, of course, it's not just Ruby. In recent years, dynamic languages have been very popular, and I personally like the simplicity of dynamic languages very much. Unfortunately, I have never spent any time on it, I also want to spend some time on other languages, such as groovy, but I am still using Ruby for the time being. Other languages have time to look at it again.

For Ruby, I am a newbie, so all the articles I have written at present are absolutely fundamental and mainly serve as a note, so you can skip BS. But fortunately, there is always a little bit of foundation, and learning will not be so painful. I made a copy of the Ruby way yesterday. I wanted to buy programming Ruby, but later I saw that way is more suitable for me, for general syntax introduction, you can just go online and check it out. The book "way" is mainly used to solve various problems and is highly practical. We learn languages to solve the problems, not to display them clearly, after a difficult decision, I finally chose the way book (the most important one is the way book, which is cheaper). Some friends say that these books can be obtained from the Internet. Why should I buy them? I can only say that this is my habit. I prefer entity books, especially good books. I must have a collection of entity books. (Sweat only for favorites .)

After I bought a book, I couldn't wait to open it. In the first chapter, I gave a rough description of the implementation of the iterator, and I thought about it. The implementation is very simple, the Code is as follows:

Def repeat (condition)
Yield
Retry if not condition
End

The repeat method accepts a condition parameter. First, run the code block. Then, if the condition is false, retry to run the code block again. This is a simple and concise implementation. It contains two important keywords: yield and retry. yield is used to execute the code block accepted by the method, this is a very important concept in dynamic languages. If you do not know this concept, Ruby will be hard to learn. It also has the same functionality in groovy and is implemented using closure closures. The retry function is to re-run the code block, of course, when not condition was established.

The book also implements an iterator that can pass parameters. The Code is as follows (why is there no ruby language in the csdn code insertion function?): def my_sequence
For I in 1 .. 10 do
Yield I
End
End

My_sequence {| x | puts x ** 3}

The above code implements an iterator to generate 1 ~ Then, call this iterator to generate the cube of the first 10 integers. The method accepts a code block {| x | puts x ** 3} with parameters, and runs the code block cyclically. After yield, I is the parameter passed to the code block.

For the first implementation above, I have made some modifications to it, as shown below: def repeat (condition)
If not condition
Yield
Retry
End
End

To be honest, I have modified this implementation according to my own understanding. I feel that this is more suitable. I tried it and the results are as expected. But I feel that this writing method is not very good, you can only learn it slowly.

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.