Dave,
Macro.to_binary is going to convert an ast to binary.
I would also suggest using the quote mechanism to generate the code you desire.
Here is an example. Assuming you have a list of tuples with the function names and their implementation, similar to the ones you get from doctest:
list = quote do: [foo: 1 + 2, bar: 3 + 5]
defs = Enum.map list, fn { name, impl } ->
quote do
def unquote(name)(_), do: unquote(impl)
end
end
contents = quote do
use ExUnit.Case, async: true
# Splice the functions in the module body.
# unquote would work just fine too
unquote_splicing(defs)
end
Module.create(MyModule, contents, __ENV__.location)
MyModule.foo(:whatever) #=> 3
You could also use Code.eval_quoted as you were, but in general Elixir provides tools so you can avoid any of the evaling functions.
José Valim
Skype: jv.ptec
Founder and Lead Developer