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:
1def function_a do
2 dbg(:function_a)
3end
4
5def function_b do
6 dbg(:function_b)
7end
8
9...
10
11def function_z do
12 dbg(:function_z)
13end
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!
1def function_a do
2 dbg(__ENV__.function)
3end
4
5def function_b do
6 dbg(__ENV__.function)
7end
8
9...
10
11def function_z do
12 dbg(__ENV__.function)
13end
https://hexdocs.pm/elixir/main/Kernel.SpecialForms.html#ENV/0
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.