Part- 4
Last updated
Last updated
You can run multiple tasks by separating them with coma ,
How ever you can also create aliases in your mix project in a file called mix.exs
.
The project definition looks like the following way when you create one using a mix
tool.
You are also allowed to add some extra fields…
Here you have to add the aliases
field.
Don’t forget to add ,
at the end when you add this in the middle of list
.
The aliases()
should return the key-value
list.
So, whenever you run the mix ecto.setup
the three tasks ecto.create
, ecto.migrate
and ecto.seed
will run one after the other.
You can also add them directly as following unlike I did with private function.
Elixir stores the documentation inside the bytecode
in a memory. You access the documentation with the help of Code.get_docs/2
function . This means, the documentation accessed when it is required, but not when it is loaded in the virtual machine like iex
Suppose you defined a module in memory like ones you defined in IEx, cannot have their documentation accessed as they do not have their bytecode written to disk.
Let us check this…
Create a module with name test.ex
with the following code. You can copy and paste it.
Now stay in the directory where your file exists and run the command
Now you can access the function definitions but not the documentation.
That means the code is compiled but documentation is not stored in the memory. So, you cannot access the docs. Lets check that…
You will see the output as nil
when you are trying to access the docs of the module you have created so far. This is because, the bytecode
is not available in disk. In simple way beam
file is not present. Lets do that...
Press Ctrl+C
twice so you will come out of the shell and this time you run the command as
After running the command, you will see a file with name Elixir.Test.beam
. Now the bytecode
for the module Test
is available in memory. Now you can access the documentation as follows...
The output is tuple with two elements. The first element is the line number of the documentation it starts and second element is the actual documentation in the binary form.
You can read more about this function here
When you go with mix test
it will run all the tests defined and gives you the time of testing. However, you can see more verbose output like which test you are running with the --trace
option like following…
It will list out the all tests with names you defined as test "test_string"
here test_string
is the name of the test.
To be simple the name of the function should be an atom instead of binary.
Executes the given command with args.
command is expected to be an executable available in PATH unless an absolute path is given.
args must be a list of binaries which the executable will receive as its
arguments as is. This means that:
Get help from iex
with h System.cmd
Checkout the documentation about System
for more information and also check Erlang os Module.
You know that when the list contains all the numbers as ASCII values, it will list out those values instead of the original numbers. Lets check that…
The code point of a
is 97
and b
is 98
hence it is listing out them as char_list
. However you can tell the IO.inspect
to list them as list itself with option char_lists: :as_list
.
Open iex
and type h Inspect.Opts
, you will see that Elixir does this kind of thing with other values as well, specifically structs and binaries.
This macro gives the current environment information. You can get the information like current filename
line
function
and others…
You can create the pid manually in Elixir with pid
function. This comes with two flavors.
Creates the pid from the string.
Creates a PID with 3 non negative integers passed as arguments to the function.
Suppose you are writing a library and you want to test one of your functions for the type pid, then you can create one and test over it.
You cannot create the pid like assigning pid = #PID<0.21.32>
because #
is considered as comment here.
When you do like above, iex shell just wait for more input as #PID<0.21.32>
is treated as comment.
Now you enter another data to complete the expression. The entered value is the value of the pid. Lets check that…
The String.replace
function will replace the given the pattern with replacing pattern. By default, it replaces all the occurrences of the pattern. Lets check that…
The String.replace str, "@", "#"
is same as String.replace str, "@", "#", global: true
But, if you want to replace only the first occurrence of the pattern, you need to pass the option global: false
. So, it replaces only the first occurrence of @
. Lets check that…
Here only first @
is replaced with #
.
You can check the memory usage (in bytes) with :erlang.memory
However, you can pass option like :erlang.memory :atom
to get the memory usage of atoms.