Looping with Ruby
Learn to use loops and iterators to automate repetitive tasks.
StartKey Concepts
Review core concepts you need to learn to master this subject
Ruby Assignment Operators
Ruby each Method
Ruby “next” Keyword
Ruby while Loop
Ruby times Method
Ruby Range
Ruby loop
Ruby until Loop
Ruby Assignment Operators
Ruby Assignment Operators
Assignment operators in Ruby are used to assign or update values to variables. The most common assignment operator is =
but others also exist, like +=
, -=
, *=
and /=
.
Loops & Iterators
Lesson 1 of 2
- 1Sometimes you want to repeat an action in Ruby while a certain condition is true, but you don’t know how many times you’ll have to repeat that action. A good example would be prompting a user for a…
- 2Did you see that? The loop printed out the numbers 1 to 10, then stopped. This was because the loop’s condition said to continue while counter was less than 11; since counter went up by 1 each time…
- 3The complement to the while loop is the until loop. It’s sort of like a backward while: i = 0 until i == 6 i = i + 1 end puts i 1. In the example above, we first create a variable i and set it…
- 4We’ve been using syntax like counter = counter + 1, which works, but as you’ll increasingly find with Ruby, there’s always another way. A shortcut is to use an assignment operator. You already k…
- 5Sometimes you do know how many times you’ll be looping, however, and when that’s the case, you’ll want to use a for loop.
- 6You saw a bit of new syntax in the previous exercise: for num in 1…10. What this says to Ruby is: “For the variable num in the range 1 to 10, do the following.” The following was to puts “#{num}”…
- 7Good work! You’re ready to build your very own for loop.
- 8So far we’ve learned one way to repeat an action in Ruby: using loops. As is often the case in Ruby, however, there’s more than one way to accomplish a given task. In this case, it’s also possible …
- 10Let’s say we want to save a range of numbers in a variable. How would we do this? A variable can only hold a single value, right? In Ruby, we can pack multiple values into a single variable using …
- 11Great work! You’re really getting the hang of this. The loop iterator is the simplest, but also one of the least powerful. A more useful iterator is the .each method, which can apply an expression…
- 12Cool, no? Now it’s your turn to take the .each method for a test drive. numbers = [1, 2, 3, 4, 5] # one way to loop numbers.each { |item| puts item } # another way to loop numbers.each do |item|…
- 13The .times method is like a super compact for loop: it can perform a task on each item in an object a specified number of times. For example, if we wanted to print out “Chunky bacon!” ten times, w…
- 14Okay, training wheels off. Let’s see your stuff! i = 3 while i > 0 do print i i -= 1 end 1. In the above example, we create a variable called i and set it to 3. 2. Then, we print out 321 sin…
- 15Good work! i = 3 while i > 0 do print i i -= 1 end j = 3 until j == 0 do print j j -= 1 end In the example above, we wrote the same loop using while and using until.
- 16In case you’re not picking up on the theme of Ruby having a gajillion ways to do any given task: let’s convert our loop yet again. for k in 1..3 print k end In the above example, we print out …
- 17Great work! We’ll give you a bit of a break from the numbers game. m = 0 loop do m += 1 print m break if m == 10 end In the example above, we print out 12345678910 since we loop 10 times.
- 18Finally, let’s replace our loop with the .times iterator.