Breakpoints in Ruby and Elixir
Like many Ruby programmers, I’ve been learning Elixir. Elixir is a functional programming language with an approachable, Ruby-like syntax. Unlike Ruby, however, Elixir is crazy fast – making it an exciting alternative for back-end programming.
One of the basic things anyone needs to learn when picking up a new language is how to set breakpoints. So, if you know how to set breakpoints in Ruby, how can you translate this into Elixir?
I’m going to break this up into three steps:
- insert your breakpoint
- trigger your breakpoint
- interact with your breakpoint
1. Insert Your Breakpoint
In Ruby you do this:
In Elixir you do this:
2. Trigger Your Breakpoint
In Ruby you do this:
In Elixir you’d do this:
NOTE: The single most important difference between breakpoints in Ruby and Elixir is that Elixir breakpoints have to be triggered from within a running Elixir REPL. This is optional in Ruby – and, indeed, most of the time you trigger your breakpoint outside of IRB, as in the example above.
This also extends to debugging in elixir tests. If you want to trigger a
breakpoint in a test file, you need to run that test within iex
like so:
The --trace
option prevents Elixir from timing out during a debugging session.
3. Interact with Your Breakpoint
At this point, enter whatever code you want to start debugging.
In Ruby you exit the pry session and skip to the next breakpoint with the
same command: quit
.
In Elixir, you exit the pry session with CTRL + C
and skip to the next
breakpoint with respawn
.