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 […] was less than 11; since […] went up by 1 each time…

  3. 3

    The complement to the […] loop is the […] loop. It’s sort of like a backward […] : […] 1. In the example above, we first create a variable […] and set it to […] (zero). 2. …

  4. 4

    We’ve been using syntax like […] , 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 know one assign…

  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 […] loop.

  6. 6

    You saw a bit of new syntax in the previous exercise: […] . What this says to Ruby is: “For the variable […] in the range […] to […] , do the following.” The following was to […] ,…

  7. 7

    Good work! You’re ready to build your very own […] 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 […] 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: […] 1. In the above example, we loop throug…

  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 […] iterator is the simplest, but also one of the least powerful. A more useful iterator is the […] method, which can apply an expre…

  12. 12

    Cool, no? Now it’s your turn to take the […] method for a test drive. […] 1. In the example above, we create an array called […] . 2. Then we show two different ways to print each ite…

  13. 13

    The […] method is like a super compact […] 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 […] ten times, we m…

  14. 14

    Okay, training wheels off. Let’s see your stuff! […] 1. In the above example, we create a variable called […] and set it to […] . 2. Then, we print out […] since we execute the loo…

  15. 15

    Good work! […] In the example above, we wrote the same loop using […] and using […] .

  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. […] In the above example, we print out […] by virtue of …

  17. 17

    Great work! We’ll give you a bit of a break from the numbers game. […] In the example above, we print out […] since we loop 10 times.

  18. 18

    Finally, let’s replace our […] with the […] 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 […] ; it takes in a string and returns an array. If we pass it a bit of text in par…

  4. 4

    All right! Time to break out our iterators to go through the user’s text. […] The example above just serves as a reminder of using […] on the […] array.

  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 […] . […] …

  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…