Skip to content

Ruby program runs with unexpected results

Sean Lerner edited this page Apr 21, 2017 · 3 revisions

In a way, a program is simply a series of steps that continually transforms state. When you encounter unexpected results, it means that the program at some point is transforming the current state in a way that you do not fully understand.

Use the following techniques to try to solve the issue:

  • follow the flow of your program's execution
  • think about the scope of any variables you're working with (local, instance, class, global)
  • think about methods you are calling, and what (if any), arguments you're providing those methods
  • think about the results methods are returning
  • think about the datatypes that you're working with
  • use puts debugging
  • use binding.pry and binding.irb to intercept and inspect your code as it's executing
  • try working out the issue inside of a REPL (irb or pry)
  • use inspect to look inside of an object
  • use raise to cause a runtime error
  • try to reproduce what you're trying to achieve in the simplest way possible, and when you have that working, incorporate it into you bigger project
Clone this wiki locally