I’ve read several chapters of Chris Pine’s “Learn to Program.” It’s a great book, easy to understand and full of humorous anecdotes and references. I still have a long way to go, but I’ve gone from learning how to define a variable to writing my own methods, which feels incredible!
There’s one aspect of programming that’s my favorite so far: Branching! Via conditional branching, using “if-then” statements, you can set up a series of outcomes that come to pass based on certain conditions.
For this type A chick, branching is like birdseed. I’m someone who likes to imagine all the possible outcomes of a situation, and I swear the circuitry of my brain has now changed to structure all my major decisions in Ruby branches.
Given Boston’s overwhelming array of dining options, this is especially handy when it comes to deciding where to eat.
def grumbling_stomach hungry
if hungry == true
puts “What would you like to eat?”
answer = gets.chomp
if answer == “Thai”
puts “Brown Sugar’s pad thai should hit the spot! Add some scallion pancakes.”
elsif answer == “Italian”
puts “Head to Rabia’s in the North End. Great seafood, amazing gnocchi.”
elsif answer == “Indian”
puts “You know how the smell of Shan-A-Punjab’s curry tickles your senses from a block away? Now’s the time to follow your nose.”
elsif answer == “Japanese”
puts “Ready to speak your dreams out loud to strangers? And also have the best ramen you’ve ever tasted? Yume Wo Katare’s where it’s at.”
elsif answer == “Ethiopian”
puts “Head to Lucy Ethiopian Cafe by the Hynes Convention Center. Have the peanut tea. Trust me.”
elsif answer == “Mexican”
puts “Anna’s Taqueria. If you disagree, we’re not friends.”
else answer == “Chocolate.”
puts “Head to Burdick’s in Harvard Square. Have a glass of water at the ready with your dark hot chocolate and plate of truffles.”
end
else hungry == false
puts “Don’t be ridiculous.”
hunger = true
grumbling_stomach hunger
end
end
#to call the method
hungry = true
grumbling_stomach hungry
As in the example above, if I set hungry to true — meaning my stomach is definitely grumbling! — I see the following when I run the program:
- “What would you like to eat?”
- A space for user input. Let’s say your cuisine of choice is Ethiopian. Type that in.
- “Head to Lucy Ethiopian Cafe by the Hynes Convention Center. Have the peanut tea. Trust me.”
If I set it to false — meaning I’m not hungry — I see this instead:
- “Don’t be ridiculous.”
- “What would you like to eat?”
- A space for user input. Let’s say your cuisine of choice is Mexican. Type that in.
- “Anna’s Taqueria. If you disagree, we’re not friends.”
You may notice this program doesn’t let you opt out of choosing a dining option. Virginia Woolf apparently said, “One cannot think well, love well, sleep well, if one has not dined well.” And I believe friends don’t let friends opt out of eating their way through the (extremely delicious) city of Boston.
Another note about this program:
In addition to branching, this program uses a programming trick called recursion, which means you can make your method call itself. This allows you to get out of extraneous coding, leaving your syntax DRY (as in Don’t Repeat Yourself).
Ways I’d like to improve this program:
I’d like to add the prompt “Are you hungry?” and allow the user to type “Yes” or “No” before progressing through the rest of the program.
I’d also like to add a branch for when the user types in an option not included in the program (i.e., “Mediterranean”).