While many Ruby developers are waiting for the promised performance improvements in Ruby 4.0, here at Twinslash we think it’s a great time to look for alternatives and support tools for server-side development.
Ruby developers should consider learning more programming languages even if for now they are satisfied with Ruby. Ruby is being developed in a slow, relaxed way, — and it’s time for something new (we think). So: let’s take a closer look at Crystal!
Crystal is a general-purpose object-oriented, statically-typed, compiled programming language with readable Ruby-like syntax. It was designed and developed in 2014, and the inventors called the language Joy — but picked another name later. Today it is developed by a community of over 300 contributors.
When creating Crystal, the developers set themselves the overarching goals of creating a symbiosis of Ruby and compiled programming languages. So Crystal got the best out of two worlds:
The language’s compiler was originally written in Ruby until it was rewritten in Crystal in 2013. In July 2016, it entered the TIOBE index. On March 22, 2021, the developers released the first stable version of Crystal into the world.
So: Crystal has a syntax similar to Ruby. The Amber framework — one of the tools that are created for the utilization of Crystal — is an MVC with a structure similar to that of Ruby on Rails.
One of the tools developed with both of these is Split-off. It’s mostly used by online e-commerce platforms like Shopify and WooCommerce. Split-off works as a plugin that can use these APIs and connect them to a shared checkout box or basket so different users who have access to it could:
All these features and inherent interoperability make Split-Off a great tool for family shopping and other cases of shared purchases. Split-Off API can work as a standalone market API for separate e-commerce solutions, for instance: the plugin has many features like messaging, payment gateway integrations (Stripe/Merchant Warrior), many service integrations (e.g., delivery companies APIs), and so on.
Unfortunately, today Crystal and Amber are very raw and have a small, compared to Ruby and Rails’, community. Libraries have bugs and need to be updated to the newer version of Crystal. The lack of maintenance for libraries and documentation and the fact language is somewhat raw make it a little bit more difficult to develop with it.
However, the already large number of startups using Ruby continues to grow, and Crystal can help significantly improve the performance of their applications. Especially since its updates are aimed at making Crystal as fast as C/C++. Undoubtedly as Crystal grows, developers will get rid of its main disadvantages — and the number of use cases utilizing Crystal and its frameworks with Ruby will increase as well.
While Ruby engineers shouldn't switch to Crystal entirely, the addition of Crystal to the project’s toolkit can boost programming significantly. Let’s discuss the advantages of introducing Crystal to your next project.
The biggest advantage of Crystal over Ruby is in how it works. Since Crystal is a compiled language before running your application the code is converted to efficient native code (using LLVM optimizations), which helps to prevent errors and bugs and increases both speed of programming and the app’s performance.
Crystal is the perfect complement to Ruby as it brings much more performance where Ruby needs it the most, while Ruby can continue to play its very dynamic role in other parts of the application.
After conducting numerous tests, developers concluded that Crystal’s performance is comparable to C. Performing the same task takes three minutes for Ruby while Crystal completes it in 12 seconds. Crystal is way faster and shows better performance within benchmarks that are comparable to C.
Just like Ruby, Crystal has readable and concise syntax, which makes its learning curve rather shallow. They are not 100% compatible but their differences are so insignificant that the learning process becomes very easy.
Crystal comes with a set of tools designed to help your dev team get started on projects quickly. Crystal compiler comes with type interference and static type checking, which prevents runtime errors — and it offers a lot of pre-written tests.
Crystal programs are compiled into executable native code, which makes development and debugging incredibly fast and straightforward. The compiler also treats Nil as type and explicitly checks all objects that can potentially be nil. That makes running into nil/null-related errors impossible with Crystal, which also boosts development speed.
Method overloading is when a code has identically named methods that are assigned with different numbers of arguments. Crystal allows developers to write such methods, and Crystal is capable of identifying which methods are being called depending on the parameter’s layout.
That makes the code more readable, but also does an incredible optimization job for programmers: they don’t have to write nil/null checks, implement code-switching, or worry about catching duck types. Crystal does a lot of that work under the hood.
What else is cool about Crystal is that it's written in Crystal (only 0.3% is written in C). So, you can easily read the Crystal source code and contribute by opening pull requests/issues.
At the same time, Ruby is written 30.6% in C and 64.8% in Ruby.
Crystal has a powerful macro system for metaprogramming. A compile-time evaluation macro system provides a lot of flexibility using Ruby's dynamic approach —yes, even though Crystal is statically typed — without Ruby’s performance penalty.
Macros in Crystal are written using Crystal (unlike, e.g. C/C++ which requires using ancient syntax and, in general, a lot of effort to program), which saves a lot of development time and allows changing characteristics of class at compile-time.
Crystal has, of course, frameworks and additional tools that help developers work more efficiently:
As you can see Crystal provides us with a decent programming ecosystem.
To get started with Crystal development you must write “Hello, world” in it:
Create a text file in: e.g., in vim:
$ crystal hello.cr
Don’t forget that Crystal uses double quotation marks for correct output. Then, let’s compile it to the native code via the build command to make it 50 times faster:
$ crystal build hello.cr
The phrase “a compiled language like Ruby'', attracts many developers to this new technology. Programming in Crystal is much easier than in Ruby, and Crystal code is also much faster.
In addition to the strong roots of Ruby, Crystal also looks to other modern languages such as Rust, Go, C#, Python, Julia, Elixir, Erlang, and Swift for inspiration. Crystal combines the best features of its predecessors in a way that no other programming language before could.
It combines the syntax and many idioms from Ruby with things like:
Crystal is also self-contained (in terms of implementation): its compiler is written in Crystal, which allows you to see this programming language in action.
The project leaders aim to create a cross-platform technology, supporting Linux, macOS, and FreeBSD for both x86/64 (64-bit) and x86 (32-bit) architectures. Also, community members of the team are actively working on the Windows port — and oh what a brave new world we will live in when it’s finally fully released.
The name Crystal speaks for itself — transparency. It has an active, majorly productive community and a lot of aspirations to become the next industry leader, — and we kind of believe in the team behind it.