A Little Bit of Logic
You know how to tell Ruby to do something if a certain condition is true, or to keep doing it while a certain condition is true. That’s what you learned in the previous two sections. But you don’t know how to tell Ruby to do something if this or that is true. In this section, you’ll learn how to say “or”, “and”, and “not” in a way that Ruby will understand.
Let’s take another look at the first branching program, here. What if my wife came home from work, saw that program, tried it, and it didn’t tell her what a lovely name she had? Help me rewrite it so I don’t look like a jerk:
1: | puts "Hello, what's your name?" |
- | name = gets.chomp |
- | puts "Hello, #{name}." |
- | |
5: | if name == "Chris" |
- | puts "What a lovely ... |
Get Learn to Program, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.