Bring your variables back from the dead!

Khaja Minhajuddin
Tripping Engineering
2 min readJun 12, 2018

--

We use a lot of Elixir at Tripping®, and using iex is a great way to explore the standard library and hex packages. However, there are times when we have to restart iex sessions. And, it is always a pain to get our variables in the right state after restarting iex, if you’ve felt the same, there’s good news for you :)

We’ve created a small package which alleviates some of this pain by allowing you to persist your variables when you close your iex session and resurrecting them when you open a new one.

Zombie Demo

Checkout the project at https://github.com/tripping/zombie

Let’s dive into the code base and see how this works. Elixir has awesome meta programming capabilities and all this can be done in less than 50 LOC.

There are 2 main macros in this file:

First is the Zombie.bury which stores the variables available in the current scope in a file named .iex_state , Elixir provides an easy way to get a hold of all the variables in the current scope using a macro called binding which returns a keyword list of variable names and their values. Now, we take this and use the :erlang.term_to_binary function to convert the whole expression into a binary which can then be saved to a file.

And the second macro is named Zombie.resurrect which reads a previously stored state file and passes it through :erlang.binary_to_term to deserialize the data from our file into Erlang/Elixir terms. Once we have these terms, it is just a matter of looping over the variable property list and creating a function for each (which returns the associated value) and finally wrapping it in a module named Zombie.Vars

That was all that was needed resurrect dead variables 😁 I’d love to see other folks do this for projects which use ruby, javascript etc.

--

--