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:
Ruby1friends = ['Alice', 'Bob', 'Charlie', 'Daniel'] 2friends.each do |friend| 3 puts "Hello, #{friend}! Nice to meet you." 4end 5# Output: 6# Hello, Alice! Nice to meet you. 7# Hello, Bob! Nice to meet you. 8# Hello, Charlie! Nice to meet you. 9# Hello, Daniel! Nice to meet you.
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.
Let's explore both of these options with 2 quick examples.
Here’s how you can print a range of numbers using a for
loop:
Ruby1for num in 0..4 2 puts num 3end 4# Output: 5# 0 6# 1 7# 2 8# 3 9# 4
In this example, the for
loop iterates over the range 0..4
, assigning each value to num
on each iteration and printing it.
Alternatively, you can achieve the same result using a .times
loop:
Ruby15.times do |num| 2 puts num 3end 4# Output: 5# 0 6# 1 7# 2 8# 3 9# 4
Here, the loop runs 5 times, printing numbers from 0 to 4. The .times
method is concise and works well when the iteration count is fixed.
The while
loop executes code continuously until a condition becomes false. Here’s a simple example:
Ruby1num = 0 2while num < 5 3 puts num 4 num += 1 5end 6# Output: 7# 0 8# 1 9# 2 10# 3 11# 4
Before each iteration, Ruby checks the condition (num < 5
). If it's true, the code block runs; otherwise, the loop exits.
Ruby loops can directly work with collections, making it easy to handle elements of an array
or string
.
Ruby1fruits = ['apple', 'banana', 'cherry'] 2fruits.each do |fruit| 3 puts fruit 4end 5# Output: 6# apple 7# banana 8# cherry
Here, the loop processes each element of the array and prints it.
Ruby1word = 'hello' 2word.each_char do |letter| 3 puts letter 4end 5# Output: 6# h 7# e 8# l 9# l 10# o
Ruby's each_char
method simplifies iterating over each character in a string by providing each character directly to the block, eliminating the need for manual indexing. It's ideal for tasks like counting vowels or transforming characters efficiently.
Loops are integral to programming. They are used in various scenarios, such as summing numbers in an array or counting specific characters in a string.
Ruby1numbers = [1, 2, 3, 4, 5] 2total = 0 3numbers.each do |num| 4 total += num 5end 6puts total 7# Output: 8# 15
In this example, the loop calculates the sum of all elements in the array.
Ruby1text = 'hello' 2vowel_count = 0 3text.each_char do |letter| 4 vowel_count += 1 if 'aeiou'.include?(letter) 5end 6puts vowel_count 7# Output: 8# 2
By combining loops with conditional statements, you can implement complex logic and solve real-world problems efficiently.
Well done on mastering Ruby loops! We revisited looping fundamentals, explored Ruby's times
, each
, and while
loops, and learned how to loop over collections like arrays and strings.
Now, it’s time to solidify your understanding through practice exercises. The more problems you solve, the more proficient you become. Keep practicing, and let’s continue our programming journey onward!