Variable bindings in IEx shell

First of all, don’t ask me why I want to do this :grinning: It was an experiment I was trying. I since decided to do it a different way, but it got me curious.

In the shell (or any other process for that matter), given a string like "time = System.os_time()" I can evaluate it with Code.eval_string("time = System.os_time()") and get back {1628697855326337000, [time: 1628697855326337000]}.

Yes, I know I can copy/paste the thing too, but humor me.

Is there a way I can programmatically inject the variable time into the shell’s variable bindings?

Basically do something like:

iex(1)> MagicModule.magic_function("time = System.os_time()")
1628697855326337000
iex(2)> time
1628697855326337000
iex(3)>

I found a really hacky way to do it in the shell

command = "foo = :ets.info(:pg2_table)"
send(self(), {:eval, Process.info(self())[:dictionary][:iex_server], command, %IEx.State{}})
2 Likes