In BlogPost, Elixir

Elixir Debugging Techniques

Coming to Elixir from Ruby, one of the things I missed most was a tool like pry.

elixir basics

Pry is a Ruby tool that you start a debugging session that enables you to see what state the program was in. I could see what values variables were initialized to and call object methods that were in the current scope.

Fortunately, Elixir provides a tool like that out of the box with IEx. Here’s how you use it.

Step 1 – require IEx

First, you have to require IEx in your program. The following is a sample usage.

Step 2 – run IEx

The file name of the sample code in Step 1 is rle.exs. So I would type the following to start a session with IEx running:

In a phoenix application, I might type iex -S mix to ensure the mix file is run.

Step 3 – Call your method

At the command prompt, I would then type RunLengthEncoder.encode(“HORSE”) as an example. Then I would see the following in my terminal:

Step 4 – respawn

Let’s say you did this to understand the value of “string” and you were now satisfied that “HORSE” was indeed passed in as the value.

You can type respawn and the program will continue executing.

Summary

This is the bare bones functionality I learned regarding debugging Elixir but if you’re coming from Ruby and are used to a tool like pry, I hope you find this to be a godsend like I did.

Recent Posts