DEV Community

Cover image for A very simple tutorial of the Crystal programming language
Andrew Garcia
Andrew Garcia

Posted on • Updated on

A very simple tutorial of the Crystal programming language

Alt Text

Want to learn a pretty new, cool and easy-to-use programming language? Crystal is a compiled language with the syntax ease of ruby and computational efficiency of C. Yes that's right, C.

Installation

right click "Open Link in New Tab" here

For Ubuntu:

On a Terminal, Copy-paste the following (3) lines:

curl -sL "https://keybase.io/crystal/pgp_keys.asc" | sudo apt-key add -
echo "deb https://dist.crystal-lang.org/apt crystal main" | sudo tee /etc/apt/sources.list.d/crystal.list
sudo apt-get update
Enter fullscreen mode Exit fullscreen mode

This will declare the files to your apt install. Then type:

sudo apt install crystal

Also copy-paste these to install additional dependencies that you will most likely need later on to build projects:

sudo apt install libssl-dev      # for using OpenSSL
sudo apt install libxml2-dev     # for using XML
sudo apt install libyaml-dev     # for using YAML
sudo apt install libgmp-dev      # for using Big numbers
sudo apt install libz-dev        # for using crystal play
Enter fullscreen mode Exit fullscreen mode

To upgrade is as simple as writing these 2 lines:

sudo apt update
sudo apt install crystal

Hello World

open a Notepad or Text Editor and Type the following in it:

puts "Hello World!"

Save the file / give it a name. You can name it helloworld.cr .cr is the extension for crystal.

Open a Terminal on the same path where you have helloworld.cr saved. If on Ubuntu and your helloworld.cr file is on your Desktop, your path would be ~/Desktop

Then while on such path type:

crystal run helloworld.cr

This shall print Hello World!

Make a project which uses shards!

This is an example I've developed. You may fork my Github repository which shows the most updated version of this example if found useful for learning purposes.

Alt Text

There are basically 2 types of projects you can create with Crystal: an application (app) or a library (lib). We will make an app. For that, go to the folder or loc where you want to create the project and on a Terminal type the following:

crystal init app firstapp

This should basically create a folder with all required files for your project. Open the created firstapp folder and you'll find something like this:

Alt Text

We are going to use num.cr as the example shard. Num is a core shard used for scientific computing in Crystal

Alt Text

If you had already clicked on num.cr shard links, you'll see that you need to add the following lines to your shard.yml file to make it work:

dependencies:
   num:
      github: crystal-data/num.cr
Enter fullscreen mode Exit fullscreen mode

Do it and save:

Alt Text

So now you need to install those declared dependencies to your project. Simply cd to your project on the terminal (cd firstapp) and then while inside firstapp, type:

shards install

If you get a red warning, you may need to install clang:

sudo apt install clang

Alt Text

Now go to the src folder and open the .cr file inside it.
In the first line of the firstapp.cr file you're gonna type the following:

require "num"

This calls/imports the num.cr shard which you've declared in the shards.yml file and built with the shards install command.

After that, you can add to the firstapp.cr file some interesting matrix objects and operations, and a lot more which the num.cr shard has to offer by going over its well-written documentation.

Happy coding πŸ¦„ 🌈

-Andrew

Top comments (0)