Can elixir's pry run private methods from within the module? Eg:
defmodule Test do
require IEx
def foo do
IEx.pry
end
defp bar do
end
end
At which point a call to bar
doesn't work. I'm very new to elixir, is there something I'm doing wrong or a reason why this is not possible?
That is the expected behavior since IEx.pry grants you access to the variables local to the functions on which it is called, but it does not put you in the scope of the module being pried.
From the IEx.pry
documentation:
When a process is pried, all code runs inside IEx and, as such, it is evaluated and cannot access private functions of the module being pried. Module functions still need to be accessed via Mod.fun(args).
To further illustrate, you can check the value of __MODULE__
. It evaluates to nil if you run it from IEx (because you're not inside a defmodule block):
iex(1)> __MODULE__
nil
If you would modify foo
to inspect the current module you would not get any surprises:
defmodule Test do
require IEx
def foo do
IO.inspect __MODULE__
IEx.pry
end
defp bar do
end
end
Now we evaluate in iex and get the appropriate result, but in pry the functions are evaluated in the IEx context (so to speak), so we would get nil again if we check the current module.
iex(1)> Test.foo
Test
# ... we skip pry ceremony
pry(1)> __MODULE__
nil
Now we can see what is going on and why we can't execute private functions from IEx.pry
I understand how this can be surprising if you come from a ruby background, since you can access pretty much whatever you want given that you evaluate a block in the context of the right object or class, but function dispatching in elixir is fundamentally different.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With