This is not a, “Which is better?”, post. It is more a way to help other developers, like myself, who have a background in Rails development and are interested in Elixir and Phoenix.
Elixir != Ruby and Phoenix != Rails. Even though there may be some similarities between both, it is beneficial to stay open to innovative and often lateral approaches in Elixir/Phoenix.
If you are a Ruby/Rails developer who is interested in adding Elixir/Phoenix to your toolbelt, then I hope this post serves as a helpful starting point.
Notice that we pass user to the full_name method. There are no “object instances” to call methods on in Elixir.
Embrace Pattern Matching
In Ruby, conditionals are predominant, but in Elixir it is more favorable to use pattern matching.
In Ruby, you might see something like this:
In Elixir, we would use pattern matching against the data structure of the user argument:
The above example shows the use of pattern matching within function definitions but we can also use pattern matching to control the flow of logic.
In Rails, this could be considered the equivalent of returning a Result object for handling outcomes beyond just true or false.
In Elixir, you would utilize pattern matching on a function’s result. The following is quite common to see in Phoenix controllers:
comments powered by Disqus