
According to theElixir documentation, an Atom is a constant whose value is own name. To some Ruby developers like me, an:atom
is like a:symbol.
In Elixir, it is common to use atoms onlists,tuples, andmaps:
defmoduleExampledodefexample_paramsdo# tuplatuple={:ok,"This is a tuple"}# listlist=[:slug,:title]# mapmap=%{name:"Diego",age:35,country:"Brazil"}endend
A curiosity in Elixir is that the booleantrue
andfalse
also is:atoms:
iex>true==:true>trueiex>false==:false>falseiex>is_atom(false)>trueiex>is_boolean(:false)>true
In Elixir, we usually useatoms as a reference status for a given request:
defmoduleExampleControllerdo#...defdelete_person(conn,%{"id"=>id})doperson=Person.get_person!(id){:ok,_person}=Person.Repo.delete(person)conn|>put_flash(:info,"Person deleted successfully.")|>redirect(to:person_path(conn,:index))endend
In the example above, the atom:ok
indicates that the delete request was executed successfully. And then a"flash message" was triggered with status:info
indicating that"the person was deleted successfully" and, after, redirected to:index
.
I hope that this content helps and makes sense to you!
Contacts
Email:contato@diegonovais.com.br
LinkedIn:https://www.linkedin.com/in/diegonovais/
Github:https://github.com/dnovais
Twitter:https://twitter.com/diegonovaistech
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse