thomasbrus

thomasbrus

Debugging via IEx

Hey!

I always find it a bit cumbersome to type require IEx; IEx.pry() whenever I need to debug something. Especially because of the lower and uppercase letters it is harder to type than for example binding.pry or byebug in Ruby.

So I figured I would create a helper function, like MyApp.pry():

defmodule MyApp do
  def pry() do
    require IEx
    IEx.pry()
  end
end

But obviously that won’t work since it will run IE.pry() inside the helper function instead of in the caller context.

So then I created this macro:

defmodule MyApp do
  defmacro pry() do
    quote do
      require IEx
      IEx.pry()
    end
  end
end

Unfortunately, now we’re back to square one though, since I’d still need to require MyApp; MyApp.pry().

So I finally settled on the following solution:

defmodule MyApp.Debugger do
  defmacro __using__(_opts \\ []) do
    quote do
      require IEx
      IEx.pry()
    end
  end
end

Which allows me to set a breakpoint anywhere using use MyApp.Debugger. Could even rename it to just use Debugger.

What do you think of this approach?

Most Liked

OvermindDL1

OvermindDL1

Any lines you add to that file (as per the linked documentation) are run upon loading IEx, so you can require whatever you want, include whatever you want, make whatever variables you want, add whatever helpers you want, etc… etc… etc… :slight_smile:

It’s in the documentation I linked in that post. :slight_smile:

josevalim

josevalim

Creator of Elixir

I like this a lot. Can you please open up an issue in Elixir’s issues tracker? Make we could support use IEx.Pry as a built-in part of core and save everyone some precious typing.

OvermindDL1

OvermindDL1

Why not just put the code you need to load in your .iex.exs file in the root project directory? :slight_smile:

Last Post!

Where Next?

Popular in Discussions Top

mbenatti
Following https://github.com/tbrand/which_is_the_fastest |> https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
restack_oslo
Hello, Please pardon me for any faux paux. I am 46 and this is my first time on a forum of any kind. I wanted to to get answers from tho...
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New
crispinb
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement