Control Flow in Ruby

Learn how to write more complex programs that can respond to user input.

Start[missing "en.views.course_landing_page.learn-ruby.course_illustration" translation]
Chevron Left Icon
Control Flow in Ruby
Lesson 1 of 2
Chevron Right Icon
  1. 1

    You may have noticed that the kinds of programs we've written so far in Ruby aren't very flexible. Sure, they can take user input, but they always produce the same result based on that input; they ...

  2. 2

    Ruby's [...] statement takes an expression, which is just a fancy word for something that has a value that evaluates to either [...] or [...] . If that expression is [...] , Ruby executes...

  3. 3

    The partner to the [...] statement is the [...] statement. An [...] / [...] statement says to Ruby: "If this expression is true, run this code block; otherwise, run the code after the [...] ...

  4. 4

    What if you want more than two options, though? It's [...] to the rescue! The [...] statement can add any number of alternatives to an [...] / [...] statement, like so: [...]

  5. 5

    Sometimes you want to use control flow to check if something is false, rather than if it's true. You could reverse your [...] / [...] , but Ruby will do you one better: it will let you use an [...

  6. 6

    In Ruby, we assign values to variables using [...] , the assignment operator. But if we've already used [...] for assignment, how do we check to see if two things are equal? Well, we use [....

  7. 7

    We can also check to see if one value is less than, less than or equal to, greater than, or greater than or equal to another. Those operators look like this: Less than: [...] Less than or eq...

  8. 8

    Great work so far! You know what they say: practice makes perfect. Let's try a few more comparators to make sure you've got the hang of this.

  9. 9

    Comparators aren't the only operators available to you in Ruby. You can also use logical or boolean operators. Ruby has three: and ( [...] ), or ( [...] ), and not ( [...] ). Boolean operat...

  10. 10

    Ruby also has the or operator ( [...] ). Ruby's [...] is called an inclusive or because it evaluates to [...] when one or the other or both expressions are true. Check it out: [...]

  11. 11

    Finally, Ruby has the boolean operator not ( [...] ). [...] makes [...] values [...] , and vice-versa. [...]

  12. 12

    You can combine boolean operators in your expressions. Combinations like [...] are not only legal expressions, but are extremely useful tools for your programs. These expression may take some ge...

  13. 13

    Great work! So far you've learned: How to use [...] , [...] , and [...] How to use comparators / relational operators like [...] , [...] , [...] , [...] , [...] , and [...] * How to...

  14. 14

    All right! You're all on your lonesome. (Well, not quite. We'll just leave this example here.) [...]

  15. 15

    Good! Now let's review the [...] statement. [...] Remember, this is basically a short hand [...] statement. It will do whatever you ask [...] the condition is [...] . In our example, [.....

  16. 16

    Now let's review comparators / relational operators. We've turned the tables a bit! Remember, comparators need to compare two values to each other to result in [...] or [...] [...]

  17. 17

    Home stretch! Let's go over boolean operators. [...] 1. With [...] both comparisons on the left and right must evaluate to [...] for the entire statement to return [...] . If the left sid...

  1. 1

    Now that we can direct our program using [...] / [...] statements, we can produce different results based on different user input. In this project, we'll combine control flow with a few new Ru...

  2. 2

    First, we should [...] a statement to prompt the user for input, then set that input to a variable using [...] .

  3. 3

    We want to make sure we capture both [...] and [...] in the user's input. We could write separate [...] / [...] statements to handle this, but we can also use [...] to convert the user's ...

  4. 4

    All right! Time to add in a little control flow. For the [...] half of our branch, we want to check whether the user's input contains an [...] . [...] We can do that using Ruby's [...] m...

  5. 5

    Good! Now let's complete our [...] statement. When we find [...] , we want Ruby to replace every instance of [...] it finds with [...] . We can do this with the [...] method, which stands ...

  6. 6

    The hard part's over! Now we just need to let the user know if we don't find any instances of the letter "s".

  7. 7

    Home stretch—now we want to display the Daffy Duckified string to the user. You can do that using the string interpolation we learned earlier: [...]

  8. 8

    Great work! How might you improve this project? You could: Add an additional [...] statement to re-prompt the user for input if they don't enter anything Think about how you might account f...

Control Flow in Ruby

Start[missing "en.views.course_landing_page.learn-ruby.course_illustration" translation]