A Couple of Things You’ll Love about Crystal Language - a Rubyist’s Perspective

Photo of Mateusz Górniak

Mateusz Górniak

Jan 8, 2018 • 6 min read

While we're still waiting for Ruby 3.0 (with the promised performance improvements) a lot of Ruby developers try to use available alternatives for the known ecosystem.

I gave a try to Crystal language some time ago and I must admit - that's great. Here are a few reasons why you should do the same. Which advantages of Crystal language might be loved by Rubyists?

Syntax

Crystal is an object-oriented programming language with heavily inspired by Ruby’s syntax. They are not 100% compatible but similar enough to make the process of learning a new language as easy as possible. All the most important differences you might find in one of the special tutorials prepared for Ruby devs or in the official book.

Performance

The biggest difference and the chance for Crystal is the way how it works. In a contrast to Ruby, Crystal is a compiled language. That means that before running your app, the code will be translated to efficient native code (using LLVM).

Handling a real-time messages systems based on WebSockets even for more popular application shouldn't be a problem. The huge performance difference is noticeable even for the smaller web apps - simple comparisons to show the scale are available on the homepages of almost every more known Crystal framework (like here).

Shards

Crystal uses dependency manager called Shard. How to use it?

 shards init

The command above creates special shard.yml file including definitions of all the dependencies used to build your new project. The default output is following:

name: test
version: 0.1.0

# authors:
#   - name <email@example.com>

# description: |
#   Short description of test

# dependencies:
#   pg:
#     github: will/crystal-pg
#     version: "~> 0.5"

# development_dependencies:
#   webmock:
#     github: manastech/webmock.cr

# license: MIT

Two the most important groups you might notice are dependencies and development_dependencies you have to extend if there's any new shard you want to use. To copy necessary files there's self-descriptive command available:

shards install

Easy, right?

Common concepts

The similarity to the Ruby language is not the only thing you may notice during a migration. There are a few still used by the community concepts.

Let's start with the frameworks.

Amber

One of the most popular frameworks created to combine high productivity of the products development and awesome performance for the written Apps. Amber uses MVC pattern and "Conventions Over Configuration" approach. It may make you feel right at home. What a good way to start the adventure!

Kemal

Lightning Fast, Super Simple web framework - that's Kemal.

require "kemal"

# Matches GET "http://host:port/"
get "/" do
"Hello World!"
end

Kemal.run

Looks familiar? Indeed, entire API is inspired by well-known "Sinatra" framework. Why might Kemal be a better choice than Sinatra? Is the performance or built-in WebSocket support important for you? I don't have to explain more :-)

ActiveRecord

There are a couple of the ORM shards already implemented but if you don't want to throw yourself in at the deep end, that's the way to go. Development of active_record.cr is still in progress - support for some features (joins, transactions) is not available yet but for a very simple app would be efficient.

Minitest

Shard created to allow to define unit tests using assertions you might know from the Ruby original minitest. Worth to try!

More possibilities

Hungry for more? All worth to see projects you may find on the special "Awesome Crystal" list or even using official CrystalShards page. Writing a game, using machine learning algorithms or even writing your own Operating System? Everything is possible now!

Summary

It's hard to compare all the aspects of both languages keeping in mind a big gap between the date of the first releases (Ruby has been created almost 20 years before Crystal language). The idea is great - using loved by millions Ruby syntax with the advantages of the statically typed programming languages. Of course, it has to take some time to make this solution more popular and production-ready but fingers crossed! I'm a huge fan!

If you want to know more, here are the links to the pages especially helpful for beginners devs:


What is your opinion? Do you see more advantages or possible problems with using Crystal? What are the next topics about Crystal language you would like to see next time?

Photo by Kira auf der Heide on Unsplash

Photo of Mateusz Górniak

More posts by this author

Mateusz Górniak

A student of Computer Science at the Poznan University of Technology who's deeply in love with...
How to build products fast?  We've just answered the question in our Digital Acceleration Editorial  Sign up to get access

We're Netguru!

At Netguru we specialize in designing, building, shipping and scaling beautiful, usable products with blazing-fast efficiency
Let's talk business!

Trusted by:

  • Vector-5
  • Babbel logo
  • Merc logo
  • Ikea logo
  • Volkswagen logo
  • UBS_Home