Debugging tip! One quick way to find out which functions you're executing.
When running tests, we sometimes want to know which functions are being executed.
You could write custom dbg/1
statements in each of the functions:
def function_a do
dbg(:function_a)
end
def function_b do
dbg(:function_b)
end
...
def function_z do
dbg(:function_z)
end
But that’s kind of a pain. It would be nicer if we could just call dbg/1
with
the current function’s name – without us having to know the name.
How can we print the name of the function without having to know it?”
Well, it turns out there’s a nice macro helper just for that! 🥳
Use the ENV macro
The __ENV__
macro has a ton of information about the current
environment. One of the things you can get is the current function.
So, you can call the function/0
function on it to get the current
function being executed!
def function_a do
dbg(__ENV__.function)
end
def function_b do
dbg(__ENV__.function)
end
...
def function_z do
dbg(__ENV__.function)
end
Pretty neat, huh?
Resources
Docs: https://hexdocs.pm/elixir/main/Kernel.SpecialForms.html#ENV/0