Elixir is usually portrayed as the successor of Ruby, primarily because it supports concurrency. On the other hand, not enough developers are familiar with this technology for it to become more widely used by businesses. Nevertheless, both Ruby and Elixir are quite popular, so we decided to take a closer look at both of them and try to answer the question of which one’s the better choice.
Ruby was created by Yukihiro "Matz" Matsumoto, who combined parts of his favorite languages, Perl, Smalltalk, Eiffel, Ada, and Lisp, to form a new language that balanced both functional and imperative programming.
Released in 1995, Ruby attracted the attention of many developers worldwide. In 2006, it gained mass acceptance, endorsed by many active user groups in major cities worldwide, and conference rooms filled to the brim.
Ruby-Talk, the main mailing list on Ruby-related topics, reached an average traffic of 200 messages per day in 2006. This declined in recent years when some of the community moved discussions from one central list to many smaller groups.
Still, Ruby is ranked among the top ten on most indexes measuring the growth and popularity of programming languages worldwide (such as the TIOBE index). This growth is due to Ruby's popularity in software written using it, particularly the Ruby on Rails framework.
Ruby is also completely free, not only to use but also to copy, modify and distribute.
Elixir is a general-purpose, functional, and concurrent programming language created in 2012 by José Valim, working initially for the Ruby on Rails team. He came up with the idea of building a new language after experiencing problems with Ruby’s improvements.
Programs written in Elixir run on an Erlang virtual machine characterized by the ability to create many processes at low cost. The concurrency model in Elixir, as in Erlang, is based on the actor model. Due to its functional nature, it emphasizes recursion and higher-order functions instead of constructs known from imperative paradigms, such as loops.
What are the differences between Ruby and Elixir?
The fundamental difference between the two starts with the very idea of their creators. In the case of Ruby, it is stressed at every step that it is optimized for the joy of programming and accepts that specific solutions used in it will not stand up to the languages at the top of the performance rankings. Conversely, Elixir - a kind of superstructure over the Erlang language - inherits a focus on programming high-reliability, long-term multi-threaded data processing applications.
Apart from that, features that allow us to differentiate between both languages are:
Ruby is an object-oriented language, while Elixir is a functional language. This is a fundamental difference because it requires a different attitude toward programming.
Different runtime environments
Elixir is executed on an Erlang VM, which allows for a completely different program execution model and affects how you write programs that use more than one processor at a time.
What do Ruby and Elixir have in common?
The languages can be perceived as cousins. Many of the tools familiar in the Ruby world have a direct counterpart in the Elixir world, e.g., Pry in Ruby is IEx.pry in Elixir, bundler is a mix, etc. Because the author mentioned above that Elixir has also been very active in developing the Ruby ecosystem, much of what's good in Ruby was the inspiration for the language and the tools in Elixir.
So, let’s get to the details:
From the very beginning, Elixir's syntax was intended to resemble that of Ruby, as it is considered by many to be very programmer-friendly.
Ruby and Elixir gain interest in similar environments. In both cases, we also focus on web development and a definite dominance of one framework.
Both languages are characterized by a high degree of flexibility thanks to extensive metaprogramming mechanisms. Library and application authors are eager to use it to create "mini-languages" (DSLs - Domain-specific languages) for a declarative, readable description of the behavior of a particular element of an application.
When is it better to choose
The advantage of Ruby is the size of the ecosystem. We can easily find libraries that will not only help us solve the most common problems and those much more exotic. Many of the tools in the Ruby ecosystem are extremely stable (e.g., Sidekiq). So it is worth considering when this is an essential factor for you. And above all, Ruby on Rails is a framework in which we will build most typical web applications faster.
Ruby is also a natural choice if you’re used to object-oriented programming based on classes. It is mature and widely adopted, although practical use of the language outside of web applications is not as widespread as, for example, in Python.
A significant advantage is that many services exposing their APIs provide consumable libraries in Ruby - it's not always so easy to find a ready-made library for Elixir.
Elixir has an objective advantage wherever we need above-average reliability and performance. Erlang is a technology often used to support telecommunications systems that have run continuously for years. It is a battle-tested solution for handling thousands of calls at a time.
Because of its focus on concurrency, Elixir will do well where it is required. A classic example would be applications that use persistent connections, such as Web Sockets. While Ruby handles them moderately at best, Elixir can maintain tens, if not hundreds, of thousands of such connections without much effort.
Elixir will also be great for writing your tools that use concurrency - scrapers, for example. It has a fantastic supervisor system taken over from Erlang, which helps manage a set of processes.
Elixir forces a developer to maintain a certain rigor and cleanliness of code by promoting the functional paradigm. This often leads to it being written at a slower pace yet better thought out. If we can afford to follow that discipline, surely this language choice will pay off.
Most frequent problems with
The most frequent issue with Ruby is its performance. However, even though there are more efficient solutions, it's hard to determine how much of a problem this is for 99% of applications.
Some say that Ruby is slow. Although true, it doesn't matter much in the web world. The bigger problem is maintaining large projects. If we develop them carelessly, it's easy to fall into a trap that leads us to have incomprehensible code that may unexpectedly fail.
Ruby's pain point is concurrent programming - because of the current Global Interpreter Lock mechanism, multithreading becomes almost useless. This is circumvented by using separate processes at the operating system level when necessary, which is an expensive and inefficient mechanism.
Elixir is still a fair niche technology, resulting in a relatively small ecosystem. It happens that developers have to write some solutions themselves, while at the same time, they are available in different technologies in the form of libraries.
Elixir is more difficult to debug than Ruby. The tools available today are limited or not very intuitive. The matter is further complicated by the macro mechanism, which makes the language so incredibly flexible, and at the same time, makes it challenging to analyze the problems that occur.
Quality of the communities
The Ruby community has existed much longer, which can be seen as both an advantage and a disadvantage. It’s easy to find outdated solutions to certain problems, so it’s better to take a closer look at the date first.
However, to a large extent, these communities intermingle. Many Elixir developers have also programmed or continue to program in Ruby. Elixir probably has a higher entry threshold due to its more demanding paradigm, but one can see recent initiatives to spread it.
So, which one to choose?
The answer to this question is not that simple, as before choosing a proper programming language, we should consider many different factors. If you worked with Ruby and have a team that knows the language well, it may not make sense to change it. Nonetheless, if you’re only at the beginning of your journey, considering Elixir is not a bad idea.
There’s also option number three - you can always choose both. The right tools will allow you to benefit from Elixir and Ruby and take advantage of their strengths. It’s all a matter of finding the best-suited solution for a project.
At Sunscrapers, we have a team of developers and software engineers with outstanding technical knowledge and experience. We can build your project successfully.
Sunscrapers can help you make the most out of all modern technologies to put your project to the next level.
Contact us at firstname.lastname@example.org