Hello, Explorer! In this lesson, we will revisit Ruby loops, essential tools that simplify repetitive tasks.
Think of loops as a playlist on repeat. We will explore the Ruby looping universe and gain hands-on experience by applying loops to collections like arrays
and strings
.
Have you ever listened to your favorite song on repeat? That's what loops are about in programming: repeating a sequence of steps efficiently. Let’s take a look at how you can greet a list of friends using an each
loop:
Here, the loop iterates through the friends
array, printing a personalized greeting for each name.
Ruby provides a for
loop to iterate over any enumerable collection. While Rubyists often prefer methods like .each
or .times
for better readability, the for
loop is straightforward and useful in certain cases.
