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
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
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.
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:
We are going to use num.cr as the example shard. Num is a core shard used for scientific computing in Crystal
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
Do it and save:
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
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)