Ruby Debugging Exercises

Ruby Debugging Exercises

Common Code Errors and How to Find Them

Many errors in code written by developers at all levels boil down to just a few common types of problems:

  1. Syntax: check brackets, if/elsif/else statements, braces, commas, parentheses, quotation marks, etc. The computer needs syntax to be exactly right in order for it to run your programs: it cannot make any assumptions about what you might mean. So it cannot, for example, infer “Wednesday” from “Wendesday.” Make sure you are using ruby syntax.
  2. Variables: make sure your variable names are consistent and that they are defined before you use them for the first time. Name them in ways that convey their use so that others (and you!) remember what they are meant to do.
  3. Functions: make sure that you know how your functions work. What kinds of things do they return? A string? An array? A hash?

If you get stuck, try the following to help you figure out what is going on:

  1. Print out variable values at different points in the program to make sure they return what you expect.
  2. Make sure the coloring in your text editor looks like you expect it to.
  3. Read the error output in the terminal. It tries to be helpful, but it will often not quite point you towards the problem. It can at least give you a ballpark location in the code and some things to look for.
  4. Get sections of the code working (see step one) so that you can narrow your search for the problem. irb can help with this: try playing in this environment to debug sections.
  5. Read the ruby documentation for the methods you are employing.
  6. Comment throughout your code to make sure you understand what every step is doing. This will help you pinpoint problems.
  7. Ask for help!

These exercises will help you practice debugging your code. There are no problems: only solutions waiting to be found.

Pirates

Broken:

Fixed:


Collections

Broken:

Fixed:


Branching

Broken:

Fixed:


Classes

Broken:

Fixed:


Grading

Broken:

Fixed:


Room 101

Broken:

Fixed: