Transition from Ruby to Elixir
Few years ago when I yet worked with legacy Rails apps, I heard about Elixir and Phoenix and their similarities to Ruby and popular open-source framework. I wanted to expand my skillset, and learn something new, believing that will make development more of joy. Functional programming sounded attractive, because I have never written functional code, except a few things in Ember or React - if that counts. Also immutability - I was hoping for less painful debugging. Last but not least this language was not mainstream, and I’ve been rather targeting niches so far. I checked out Elixir and here is my short write-up about my experience.
My Elixir Path
The few first months of learning Elixir and functional programming were about writing small prototypes. My friend challenged me with his pet project idea and I asked him if I can do it in Elixir. Sometimes it was a struggle because it was a solo journey, but it was worth doing exercise, just to explore the ecosystem. I also joined Slack group and asked questions, when I’ve been stuck. Folks in the community were helpful many times. They clarified my thoughts, and allowed me to get moving again.
Joining an Exercism helped me to speed up with grasping concepts of functional programming and solve more advanced problems - basically I tried to collect all medium level challenges, doing them regularly. It was quite good to have access to the solutions of experienced developers from which I could learn how I could do it better. This path of learning gave me the most fun (read challenge). Here is an example of how complex problem can be solved with less than 70 lines of code (are you football fan?!):
defmodule Tournament do
Above you can see pattern matching in action (
score_match), pipe operator inside
Enum.reduce/3 which I used very often in my Exercism solutions. Notation of Elixir functions was a bit weird for me on the beginning. Simply, Elixir has the same function taking different number of arguments, unlike Ruby. So we have
Map.merge/3 which can resolve conflicts through the given fun, see
Team | MP | W | D | L | P
It ain’t magic
After moving from Vim to VSCode I was missing good support for Ruby and was constantly battling with my editor when working on larger projects, especially those which had a client part inside an Rails repo. I found extension called ElixirLS, it was super handy. Compiler warnings and errors (undefined functions) have been an enormous help as well. Compared to Ruby, where you get errors at runtime, it has been an advantage. In general, less magic is happening in Elixir/Phoenix in my opinion (thanks to immutability, Ecto syntax closer to SQL, some folks say - more modular framework). It means fewer places in how magic behaves to bite you. To clarify what I mean, have a look at this example from old loved Ruby:
t = Time.now
How about tools?
Elixir does not have so many libraries as Ruby (which can be good! - less choice is more happiness ;)). Packages that are available are mature (with a few exceptions of course!). Sometimes (well, often!) you will need to build a HTTP API client from scratch, or with partial help of existing packages. Check on how you can use functions that another developer has already implemented for your convenience, how to use macros in Elixir. I found that google_gax is using macros, for example, here: https://github.com/balena/elixir-google-gax/blob/master/lib/google_api/gax/model_base.ex#L63.
I believe you will get productive quickly with a solid Ruby (Python, JS, Java, or Erlang :)) background. If you need concurrency and performance, Elixir would be a great option too, however that’s not the topic of this post. Also, worth mentioning types checking on compile time and tools like
For anyone interested in learning Elixir, I would recommend thinking of a project that excites you, rather than doing tutorials. By having a goal to build a prototype, you will be more motivated than as you would by copy-pasting the code. Elixir is niche language however I hear that more systems are migrated from
- Credits: Code has been taken from Exercism, link: elle’s solution