Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for What is an atom in Elixir?
Diego Novais
Diego Novais

Posted on • Edited on

     

What is an atom in Elixir?

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
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Senior Software Engineer | Ruby | Ruby On Rails | Elixir | Phoenix | Technical Writer | LLM
  • Location
    Brazil
  • Joined

More fromDiego Novais

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp