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

PragTob
Hey everyone, this has been on my mind for some time and I’d love your input on it! TLDR: I feel like maps are superioer for storing and...
New
AstonJ
I’ve just started the Phoenix part of the utterly brilliant online course by @pragdave. On generating the Phoenix app he uses the --no-ec...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
tomekowal
Hey guys! I want to create a toy project that shows a chart of temperature over time and updates every 5 seconds. I feel LiveView is per...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New
nunobernardes99
Hi there Elixir friends :vulcan_salute: In a recent task I was on, I needed to check in two dates which of them is the maximum and which...
New
fireproofsocks
I’ve been working on an Elixir project that has required a lot of scripting. I usually reach for Elixir because I like it more (and in th...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement