ruby · level 1 · unit 7 · lesson 4
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Big programs are small pieces working as a team.
Real programs are built from small parts. An array holds the data. A loop walks through it. A method answers one small question, and an if decides what to do with each answer.
Here, tasty? ends with a question mark, like even?, because it answers yes or no. Its last line, rating >= 7, hands back true or false, ready for the if.
When something goes wrong, you : find the bug, fix it, run again. Every programmer does this every day. Small methods help, because you can test each one on its own.
def tasty?(rating)
rating >= 7
end
[9, 3, 8, 5].each do |rating|
puts "#{rating} out of 10. Yum!" if tasty?(rating)
end Test each method on its own first, then put the pieces together.
guess first
Every piece at once. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
def double(number)
number * 2
end
[1, 2, 3].each do |goal|
puts double(goal) if goal.odd?
end
your turn
Your turn. Plan a trip to the snack shop.
Put three snack prices in the array, then type how much money you have in the last blank. Make sure you can afford at least one snack!
fix it
Only one Paris counts as right. Debug it!
- Only one guess counts as right. What is different about the other two?
- Ruby is case-sensitive, so Paris and paris are different strings. Change the guess before comparing.
- On line 2, change guess to guess.downcase, then press run.
nailed it
you put all the pieces together. :D
- Small methods, each with one job, build big programs.
- Arrays hold data, loops walk through it, and
ifdecides. - To debug, find the bug, fix it, then run again.
new words in your collection
- debug
This lesson isn’t saved. A free account remembers every lesson you finish.