Evgenii Legotckoi
June 25, 2016, 12:22 p.m.

User Guide #09 - Ruby - iterators

Iterators are not an original concept with ruby. They are in common use in object-oriented languages. They are also used in Lisp, though there they are not called iterators. However the concepet of iterator is an unfamiliar one for many so it should be explained in more detail.

The verb iterate means to do the same thing many times, you know, so an iterator is something that does the same thing many times.

When we write code, we need loops in various situations. In C, we code them using for or while. For example,

  1. char *str;
  2. for (str = "abcdefg"; *str != '\0'; str++) {
  3. /* process a character here */
  4. }

C's for(...) syntax provides an abstraction to help with the creation of a loop, but the test of str against a null character requires the programmer to know details about the internal structure of a string. This makes C feel like a low-level language. Higher level languages are marked by their more flexible support for iteration. Consider the following sh* shell script:

  1. #!/bin/sh
  2.  
  3. for i in *.[ch]; do
  4. # ... here would be something to do for each file
  5. done

All the C source and header files in the current directory are processed, and the command shell handles the details of picking up and substituting file names one by one. I think this is working at a higher level than C, don't you?

But there are is more to consider: while it is fine for a language to provide iterators for built-in data types, it is a disappointment if we must go back to writing low level loops to iterate over our own data types. In OOP, users often define one data type after another, so this could be a serious problem.

So every OOP language includes some facilities for iteration. Some languages provide a special class for this purpose; ruby allows us to define iterators directly.

Ruby's String type has some useful iterators:

  1. ruby> "abc".each_byte{|c| printf "<%c>", c}; print "\n"
  2. <a><b><c>
  3. nil

each_byte is an iterator for each character in the string. Each character is substituted into the local variable c. This can be translated into something that looks a lot like C code ...

  1. ruby> s="abc";i=0
  2. 0
  3. ruby> while i<s.length
  4. | printf "<%c>", s[i]; i+=1
  5. | end; print "\n"
  6. <a><b><c>
  7. nil

... however, the each_byte iterator is both conceptually simpler and more likely to continue to work even if the

  1. String
class happens to be radically modified in the future. One benefit of iterators is that they tend to be robust in the face of such changes; indeed that is a characteristic of good code in general. (Yes, have patience, we're about to talk about what classes are, too.)

Another iterator of String is each_line.

  1. ruby> "a\nb\nc\n".each_line{|l| print l}
  2. a
  3. b
  4. c
  5. nil

The tasks that would take most of the programming effort in C (finding line delimiters, generating substrings, etc.) are easily tackled using iterators.

The for statement appearing in the previous chapter does iteration by way of an each iterator. String`s each works the same as each_line , so let's rewrite the above example with for :

  1. ruby> for l in "a\nb\nc\n"
  2. | print l
  3. | end
  4. a
  5. b
  6. c
  7. nil

We can use a control structureb retry in conjunction with an iterated loop, and it will retry the current iteration of the loop from the top.

  1. ruby> c=0
  2. 0
  3. ruby> for i in 0..4
  4. | print i
  5. | if i == 2 and c == 0
  6. | c = 1
  7. | print "\n"
  8. | retry
  9. | end
  10. | end; print "\n"
  11. 012
  12. 01234
  13. nil

yield occurs sometimes in a definition of an iterator. yield moves control to the block of code that is passed to the iterator (this will be explored in more detail in the chapter about procedure objects). The following example defines an iterator

  1. repeat
, which repeats a block of code the number of times specified in an argument.

  1. ruby> def repeat(num)
  2. | while num > 0
  3. | yield
  4. | num -= 1
  5. | end
  6. | end
  7. nil
  8. ruby> repeat(3) { print "foo\n" }
  9. foo
  10. foo
  11. foo
  12. nil

With retry, one can define an iterator which works the same as while, though it's too slow to be practical

  1. ruby> def WHILE(cond)
  2. | return if not cond
  3. | yield
  4. | retry
  5. | end
  6. nil
  7. ruby> i=0; WHILE(i<3) { print i; i+=1 }
  8. 012 nil

Do you understand what an iterator is? There are a few restrictions, but you can write your original iterators; and in fact, whenever you define a new data type, it is often convenient to define suitable iterators to go with it. In this sense, the above examples are not terribly useful. We can talk about practical iterators after we have a better understanding of what classes are.

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • Evgenii Legotckoi
    April 16, 2025, 5:08 p.m.
    Благодарю за отзыв. И вам желаю всяческих успехов!
  • IscanderChe
    April 12, 2025, 5:12 p.m.
    Добрый день. Спасибо Вам за этот проект и отдельно за ответы на форуме, которые мне очень помогли в некоммерческих пет-проектах. Профессиональным программистом я так и не стал, но узнал мно…
  • AK
    April 1, 2025, 11:41 a.m.
    Добрый день. В данный момент работаю над проектом, где необходимо выводить звук из программы в определенное аудиоустройство (колонки, наушники, виртуальный кабель и т.д). Пишу на Qt5.12.12 поско…
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…