We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
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! 🥳
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
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.