Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Module name to String and back to module

Tags:

elixir

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.

like image 483
script Avatar asked Mar 19 '18 09:03

script


People also ask

How do I change the name of a module?

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.

How do I use import_module in the importlib?

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.

How do I remove a module or form?

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)

What is module1 and module2 and module3 in VBA?

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.


2 Answers

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
like image 108
Dogbert Avatar answered Oct 09 '22 18:10

Dogbert


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
like image 23
Marc-André Lafortune Avatar answered Oct 09 '22 16:10

Marc-André Lafortune