If we have a module name like this:
Module.V1.CountryTest
I can convert it to String like this:
Module.V1.CountryTest |> to_string
Now there are some interesting results I am getting on iex
module = Module.V1.CountryTest |> to_string
"Elixir.Module.V1.CountryTest"
iex(2)> replace = Regex.replace(~r/Test/, module, "")
"Elixir.Module.V1.Country"
iex(3)> replace |> String.to_atom
Module.V1.Country
So if I remove Test
. And convert it back to atom
. It will give me back the module name. But If I replace
or remove
anything else from the module name it gives me this output:
some = Regex.replace(~r/Country/, replace, "")
"Elixir.Module.V1."
iex(5)> some |> String.to_atom
:"Elixir.Module.V1."
Can anybody please explain this behavior? And why it wont allow any other parts to change or replace. Meaning giving me back the output like this
Module.V1.Country
I mean if its possible.
Thanks.
Select the name property in the Properties Window (a module will only have this property; a form has multiple properties) 3. Delete the module name (in this case Module1) and type in the name you want to call your module. 4. Press enter to rename the module.
The importlib provides a simple import_module function that accepts a string name with dot separators. It works just like a normal import except it uses a string to store the name. On import, the file is executed and the module object is returned. If you only need the code run on import it is not necessary to store the returned module.
Right-click on the module or form you wish to remove to show the right click short cut menu. Click Remove (in this case Module2)
The first time you insert a module in VBA, it will automatically be given the name of “ Module1 ” and subsequent module will become Module2, Module3 etc. Similarly, when you insert a user form it will be called UserForm1, UserForm2 etc.
Elixir module names are just atoms prefixed with "Elixir."
. Elixir prints atoms which start with "Elixir."
and contain a valid Elixir module name after that differently than other atoms:
iex(1)> :"Elixir.Foo"
Foo
iex(2)> :"Elixir.F-o"
:"Elixir.F-o"
When you replace Test
, the rest of the value is a valid Elixir module name, but when you replace Country
as well, you end up with a .
at the end which is not a valid module name. If you remove the dot too, you'll get what you want:
iex(3)> Module.V1.Country |> to_string |> String.replace(~r/Country/, "") |> String.to_atom
:"Elixir.Module.V1."
iex(4)> Module.V1.Country |> to_string |> String.replace(~r/\.Country/, "") |> String.to_atom
Module.V1
To convert a string to a module, you may want to use Module.safe_concat/1
or /2
:
Module.safe_concat(Module.V1, "CountryTest") # => Module.V1.CountryTest
Module.safe_concat(~w[Module V1 CountryTest]) # => Module.V1.CountryTest
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With