__MODULE__ --> how to get the root part of it?

Given a nested module “A1.B1.C1”. If I call__MODULE__ from within it, it’ll return “A1.B1.C1”

How can I get the root “A1” dynamically?

How about this?

__MODULE__ |> String.split(".") |> List.first()

The safe way:

iex(4)> A1.B1.C1 |> Module.split() |> List.first() |> List.wrap() |> Module.safe_concat()
A1

Remember, modules are ‘flat’. There isn’t a C1 inside of a A1.B1, the module is literally named A1.B1.C1, the dots are part of it and all.

This version will crash, you can’t split an atom.

4 Likes