Crystal tuples: the immutable data structure of crystal

Bookmark and Share

Tuple is a data structure which has immutable elements and is of a fixed size. Its similar to an array, but unlike arrays in crystal and ruby which allows adding more values over time a tuple is of fixed and cannot change. (Disclaimer: This article is meant for Ruby developer and explaining what a tuple is to a ruby developer).

In crystal we have two types of tuple

1) Tuple {1, “hello”, :world}
2) NamedTuple {x: 1, y:2, z: 5}

They are immutable, which means if you try changing the value of an element in a tuple you will get an exception. Since crystal programs are compiled before execution you will get to see these errors while you compile the program itself.


If you try assigning a value to it, like so

You will get an exception like bellow.

In crsytal, tuples are the preferred way to return a multiple results from a method.

For example inside the crystal core we have a method to get the minimum and maximum of an array.

the result would be {1, 100}

Note: Since we just mentioned minmax, have a look at minmax_by method as well. It would let you apply a block of code over your range and then …

Read More

Web Frameworks in Crystal

Bookmark and Share


The creator of Ruby, Yukihiro Matsumoto said that the language was made to make programmers happy. Imagine a language as elegant as Ruby and blazingly fast as C. If you already love Ruby, maybe it is time to start considering Crystal lang, the younger sibling of Ruby. It is a compiled language which is syntactically very similar to Ruby and designed for high throughput and performance.


  • Native WebSocket support
  • Compiles to efficient native code
  • Statically typed => if any errors it will let you know at compile time.
  • Less memory consumption

Web frameworks in Crystal

Applications developed using framework are easy to maintain and upgrade at a lower cost. This article lets you get familiar with some of the most popular frameworks of Crystal.

Install Crystal to get started.

Create our Crystal app


To know the true potential of Crystal, let’s familiarise with Kemal, most popular Crystal framework. It’s a Sinatra inspired framework.

Install Kemal

In our app,

Open in an editor and add Kemal as a dependency in the shard.yml file.

To get dependencies, run

This will install Kemal.

This is similar to adding gems in gem file and bundle install in Rails.

In the file created in the src directory, substitute the following

In the …

Read More

Painless Cron jobs in Crystal using Schedule

Bookmark and Share

Many Ruby developers use the awesome whenever gem for scheduling tasks in their projects, so do we and with ‘whenever’  scheduling tasks become so effortless that we absolutely loved it. Recently we have been deploying Crytal apps to production and Kemal is our framework of choice. During the process, we felt the urge to have something similar to whenever in Crystal for cron jobs and that’s when we discovered Schedule – a Crystal shard that provides a clear DSL to write periodic or scheduled tasks and there was no turning back.

Getting started

Add Schedule to your shard.yml file

you are all set to schedule your tasks, all you gotta do is require the schedule module as require “schedule”


Schedul’s API defines 2 important methods, .every and .after for periodic and scheduled execution of tasks respectively.

Periodic execution

For running a task periodically we have to pass in a valid interval as well as a block to the .every method. For example,

will print the message on every 3 seconds.


will call the class method hourly_job every 1 hour

Scheduled execution

For scheduling tasks to run sometime in the future we can make use of the after method.

Stop and Retry jobs

Call Schedule.retry to retry a task and …

Read More

Deploying Crystal app to production using Heroku

Bookmark and Share

This article assumes that you have Crystal and Heroku CLI installed.

Create a sample application

Create a new application using

You should see the following on your terminal

now cd to the root of the app, $ cd sample-app  and add Kemal to shrad.yml file

and run $ crystal deps for installing dependencies.

Add a “/” route

Open file and add replace the contents with the following code

now run $ crystal src/  and go to http://localhost:3000on your browser. You should see ur app running and the message ‘Hello World’.

Deploy to Heroku

So far so good 🙂 Now let us deploy our app to Heroku. Run

$ heroku create your-custom-name --buildpack

You should see something like the following on your terminal.

You are all set. Commit and push to heroku

To see your app running go to . Congratulations you just deployed your crystal app to production

Happy Coding

Read More