Reading error messages
Errors belong to programming like splashes belong to painting – even professionals see dozens of them every day. The difference: they read the message. Because Ruby tells you surprisingly precisely what's wrong. An error message has three parts: the kind of error (a class like NameError), a description and the line where it happened – in the notebook Ruby marks it red.
Most cells of this lesson go wrong on purpose. The most common error of all is a typo:
name = "Chunky"
nmae.upcaseNameError: undefined local variable or method 'nmae' for main means: Ruby doesn't know the name nmae – neither as a variable nor as a method. Most of the time it's a typo, and right under the message Ruby suggests what you might have meant. The second common cause in the notebook: the variable comes from a cell further up that you haven't run yet.
42.upcaseNoMethodError: undefined method 'upcase' for an instance of Integer – the method exists, just not for this kind of object: you can't capitalize a number. The question then is: what kind of object do I actually have here? .class tells you.
The trickiest variant of this error mentions nil:
animal = { name: "Chunky", food: "bacon" }
animal[:fod].upcaseundefined method 'upcase' for nil – the problem isn't upcase but the nil: animal[:fod] found nothing, because the key has a typo. Whenever a message mentions nil, ask yourself: where does that nil come from? A p shows you what's really in a value:
p animal[:fod]
p animal[:food]Even better: don't let the error travel in the first place. fetch reports a missing key at once – right where it's missing, instead of three lines later as a puzzling nil. And here, too, you get a suggestion:
animal.fetch(:fod)On to an error that mixes up text and numbers:
amount = 3
"Bacon: " + amountTypeError: no implicit conversion of Integer into String – Ruby won't secretly turn a number into text to attach it to a string. Say so explicitly: with interpolation "Bacon: #{amount}" or with amount.to_s.
def square(number)
number * number
end
square(3, 4)ArgumentError: wrong number of arguments (given 2, expected 1) – the method wants one argument, it got two. Look up how the method is defined.
And then there are errors where Ruby doesn't even understand the code – the SyntaxError. An end is missing here:
if 3 > 2
puts "Bacon!"With a syntax error not a single line runs. Ruby points with ^ at the places where it got stuck: at the if that never gets its end, and at the end of the code, where the end should have come at the latest. Tidy indentation helps you spot this at once. The most important errors at a glance:
| Error | What's usually behind it |
|---|---|
NameError | a typo in the name, or the cell that creates the name hasn't run yet |
NoMethodError | the method doesn't exist for this object – often it's an unexpected nil |
TypeError | two kinds of values don't fit together, such as a string and a number |
ArgumentError | too many or too few arguments, or a value the method can't work with |
ZeroDivisionError | a whole number was divided by 0 |
KeyError | fetch with a key that doesn't exist |
SyntaxError | Ruby doesn't understand the code: a missing end, bracket or quote |
When things blow up: 1. Read the whole message – error class and text. 2. Go to the marked line. 3. Use p to check what the values there really are. And if nothing helps, paste the message into a search engine – you are guaranteed not to be the first person with this error. How your own programs catch errors instead of crashing is shown right away in lesson 29, the next one.
Chunky eats 3 strips of bacon. – but it has three bugs. Run it, read the message, repair the marked spot, and repeat until everything runs.fox = { name: "Chunky", strips: 3 }
def sentence(animal)
"#{animal[:name]} eats " + animal[:strips] + " strips of bacon."
end
sentence(foxx, 3)
1. Nothing is red: Ruby does exactly what the code says. Put a p total into the loop and watch what happens to total; the last line hides a second mistake. Fix both until average is 4.25.strips = [3, 5, 2, 7]
total = 0
strips.each do |s|
total = s
end
average = total / strips.length