Skip to content

Instantly share code, notes, and snippets.

@sofakingworld
Last active March 25, 2018 15:18
Show Gist options
  • Save sofakingworld/352545b6623cd94e212085578ad0b335 to your computer and use it in GitHub Desktop.
Save sofakingworld/352545b6623cd94e212085578ad0b335 to your computer and use it in GitHub Desktop.
Meta Elixir (Create methods dynamic)
defmodule Dynamic do
@methods ~w(method1 method2 method3)a
@methods
|> Enum.each( fn method ->
method_name = "prefix_#{method}"
def unquote(:"#{method_name}")(argument1, argument2) do
{unquote(method), argument1, argument2}
end
end)
end
@sofakingworld
Copy link
Author

sofakingworld commented Mar 25, 2018

iex> Dynamic.prefix_method1 1, 3
=> {:method1, 1, 3}
iex> Dynamic.prefix_method2 :_some_atom, [1, 2, 3]
=> {:method2, :some_atom, [1, 2, 3]}
iex> Dynamic.prefix_method3 nil, nil
=> {:method3, nil, nil}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment