Interest Ask. There are several ways to loop in Ruby. I noted that in Ruby there is a development principle where there are many ways to do the same, there are usually subtle differences between them, and each case has its own unique use, its own problem, which it solves. Therefore, in the end, you need to write (and not just read) all of them.
Regarding the for loop question, this is similar to my earlier question, because the loop is a trap .
Basically, there are two main explicit methods of cyclization: one of them is iterators (or, more generally, blocks), such as
[1, 2, 3].each { |e| puts e * 10 } [1, 2, 3].map { |e| e * 10 )
Connected to this iteration method is the Enumerator class, which you should try to understand.
Another way is to loop Pascal-ish using while , until and for loops.
for y in [1, 2, 3] puts y end x = 0 while x < 3 puts x; x += 1 end
Like if and unless , while and until have a tail shape such as
a = 'alligator' a.chop! until a.chars.last == 'g' #=> 'allig'
The third very important way to loop is an implicit loop, or a loop through recursion. Ruby is extremely flexible, all classes can be changed, hooks can be customized for various events, and this can be used to create the most unusual ways to loop. The possibilities are so endless that I don’t even know where to start talking about them. Perhaps a good place is the blog post by Yusuke Endoh , a renowned artist working with Ruby code as his own piece of art of choice.
To demonstrate what I mean, consider this cycle
class Object def method_missing sym s = sym.to_s if s.chars.last == 'g' then s else eval s.chop end end end alligator