Key Concepts

Review core concepts you need to learn to master this subject

Ruby while Loop

Putting a block of code in a while loop in Ruby will cause the code to repeatedly run the code as long as its condition is true.

If the block of code doesn’t have a way for the condition to be changed to false, the while loop will continue forever and cause an error.

Ruby until Loop

Putting a block of code inside an until loop in Ruby will cause the code to run as long as its condition remains false. It’s only when the condition becomes true that the loop stops.

If the block of code doesn’t allow for a way for the condition to be changed to true then the loop will continue forever and it will cause an error.

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 /=.

Ruby for Loop

A block of code can be repeated a set amount of times with the for loop in Ruby.

Ruby Range

In ruby, a sequence of integers can be demonstrated by a range. The range can be divided into an inclusive range where the last integer in the sequence is included and an exclusive range where the last integer is excluded.

Ruby loop

A loop method can be used to run a block of code repeatedly in Ruby. Either use curly braces ({}) or the do/end keyword combination to wrap the block the code that will be looped.

Ruby each Method

To iterate over an array in Ruby, use the .each method. It is preferred over a for loop as it is guaranteed to iterate through each element of an array.

Ruby times Method

To execute the same block of code a set a number of times in Ruby, use the times method.

Chevron Left Icon
Loops & Iterators
Lesson 1 of 2
Chevron Right Icon
  1. 1
    Sometimes 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…
  2. 2
    Did 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…
  3. 3
    The 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…
  4. 4
    We’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…
  5. 5
    Sometimes 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.
  6. 6
    You 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}”…
  7. 7
    Good work! You’re ready to build your very own for loop.
  8. 8
    So 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 …
  9. 9
    The next keyword can be used to skip over certain steps in the loop. For instance, if we don’t want to print out the even numbers, we can write: for i in 1..5 next if i % 2 == 0 print i end …
  10. 10
    Let’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 …
  11. 11
    Great 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…
  12. 12
    Cool, 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|…
  13. 13
    The .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…
  14. 14
    Okay, 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…
  15. 15
    Good 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.
  16. 16
    In 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 …
  17. 17
    Great 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.
  18. 18
    Finally, let’s replace our loop with the .times iterator.
  1. 1
    Hiding information is a major part of programming: protecting passwords, establishing secure connections, and securing programs against tampering all rely on controlling access to information. Whi…
  2. 2
    First things first: we’ll need to get the user’s input.
  3. 3
    Next, we’ll want to divide the user’s input into individual words. Ruby has a built-in method for this called .split; it takes in a string and returns an array. If we pass it a bit of text in pare…
  4. 4
    All right! Time to break out our iterators to go through the user’s text. letters = [‘a’, ‘b’, ‘c’, ‘d’] letters.each do |letter| print letter end The example above just serves as a reminder o…
  5. 5
    Good! There were two problems with our output, though: we didn’t have spaces between our words, and our program didn’t actually replace the word we wanted to redact with the word “REDACTED”. if va…
  6. 6
    Perfect! Feel free to mess around with your redactor to see what sorts of results you get. Think about the following: * What could you do to make sure your redactor redacts a word regardless of wh…