Crystal Programming Language

Crystal Programming Language

What's the Scoop?

An unexpected lesson with Crystal

Have you ever used scoop? An odd question perhaps, but I never heard of or knew about it until today. Yet another tangent I slipped into while working with devdocs.io and noticing "Crystal, a statically typed, ruby-like, compiled language."

I decided to install and play around with Crystal for an afternoon to learn a little more about the language. To do so, I had to install scoop and aria2 first. Check out Scoop's Readme or refer to the Wiki for more information.

Version 1.0 of Crystal was unveiled on March 22, 2021. As of this writing, Crystal is at version 1.50 and it's still not quite ready for Windows, but it's close. I installed the "Windows Preview" version for now.

A version is also available for the Linux Kernel which can be enabled using Windows Features "Turn Windows features on or off." Check the box next to "Windows Subsystem for Linux" as pictured below:

Enable Windows Subsystem for Linux - Screenshot 2022-07-10 133712.png

Check the box, click OK, and reboot your machine for the change to take effect.

I installed scoop and aria2, and time will tell if I did the right thing or not. In the meantime, Crystal piqued my interest enough to take a break from some of my other not-so-urgent projects.

Once scoop and aria2 are installed, it's an easy task to install crystal.

scoop bucket add crystal-preview https://github.com/neatorobito/scoop-crystal
scoop install crystal

Updating crystal is just as easy:

scoop update
scoop update crystal

The following is a minimally edited copy of my PowerShell session while installing scoop, aria2, and crystal on my Windows 11 machine.

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows                                                                                                                                                       

> Set-ExecutionPolicy RemoteSigned -Scope CurrentUser                                                  > irm get.scoop.sh | iex
Initializing...                                                                                                         Downloading...
Extracting...
Creating shim...
Adding ~\scoop\shims to your path.
Scoop was installed successfully!                                                                                       Type 'scoop help' for instructions.

> scoop install aria2
Installing 'aria2' (1.36.0-1) [64bit]
aria2-1.36.0-win-64bit-build1.zip (2.3 MB) [==================================] 100%
Checking hash of aria2-1.36.0-win-64bit-build1.zip ... ok.
Extracting aria2-1.36.0-win-64bit-build1.zip ... done.
Extracting aria2-1.36.0-win-64bit-build1.zip ... done.                                                                  Linking ~\scoop\apps\aria2\current => ~\scoop\apps\aria2\1.36.0-1                                                       Creating shim for 'aria2c'.
'aria2' (1.36.0-1) was installed successfully!                                                                          

> scoop bucket add crystal-preview https://github.com/neatorobito/scoop-crystal                        Checking repo... OK
> scoop install crystal
WARN  Scoop uses 'aria2c' for multi-connection downloads.
WARN  Should it cause issues, run 'scoop config aria2-enabled false' to disable it.
WARN  To disable this warning, run 'scoop config aria2-warning-enabled false'.
Installing 'vswhere' (3.0.3) [64bit]
Starting download with aria2 ...
Download: Download Results:
Download: gid   |stat|avg speed  |path/URI
Download: ======+====+===========+===============================
Download: 37125b|OK  |   123KiB/s|C:/Users/Redge/scoop/cache/vswhere#3.0.3#https_www.nuget.org_api_v2_package_vswhere_3.0.3_vswhere.zip
Download: Status Legend:
Download: (OK):download completed.
Checking hash of 3.0.3 ... ok.
Extracting vswhere.zip ... done.
Linking ~\scoop\apps\vswhere\current => ~\scoop\apps\vswhere\3.0.3
Creating shim for 'vswhere'.
'vswhere' (3.0.3) was installed successfully!
Installing 'crystal' (1.5.0) [64bit]
Starting download with aria2 ...
Download: Download Results:
Download: gid   |stat|avg speed  |path/URI
Download: ======+====+===========+===============================
Download: 8507c7|OK  |   107KiB/s|C:/*****/*****/scoop/cache/crystal#1.5.0#https_github.com_crystal-lang_crystal_releases_download_1.5.0_crystal-1.5.0-windows-x86_64-msvc-unsupported.zip
Download: Status Legend:
Download: (OK):download completed.
Checking hash of crystal-1.5.0-windows-x86_64-msvc-unsupported.zip ... ok.

Extracting crystal-1.5.0-windows-x86_64-msvc-unsupported.zip ... done.
Linking ~\scoop\apps\crystal\current => ~\scoop\apps\crystal\1.5.0
Creating shim for 'crystal'.
Creating shim for 'shards'.
Running post_install script...
'crystal' (1.5.0) was installed successfully!
>

My first Crystal Program - Hello, World

I created a subdirectory c:\code\crystal and then entered the command crystal init app hello_world to initialize the hello_world application. The source code for the application is in the "src" subdirectory.

Crystal Hello_World Subdirectory - Screenshot 2022-07-09 222115.png

I modified the code in Hello_World.cr using Sublime Text 4. This editor has a package that supports the Crystal Programming language. As much as I enjoy using Sublime Text 4, Microsoft's Visual Studio Code is my preferred choice and offers a Crystal plugin as well and makes it easy to work with WSL Ubuntu.

# TODO: Write documentation for `HelloWorld`
module HelloWorld
  VERSION = "0.1.0"

  # TODO: Put your code here
  puts "Hello, World!"
end

After entering the file, enter Crystal build hello_world.cr to create a hello_world.exe (executable) file.

Crystal Build Hello_World.exe 2022-07-09 222743.png

It only takes a few lines of code to create a server instance! The code (I'm using Sublime Text 4 with a Crystal plugin) and the browser appear side by side below:

Crystal Server - Screenshot 2022-07-10 032241.png

Crystal Performance

The time test (see below) suggests that Crystal is not as fast as C, however, it did beat out Ruby and Python (slowest).

Code written in Crystal is expressive and safe, but it's also fast – really fast. Once built, it goes head to head with other low-level languages such as C, C++, or Rust, according to Crystal Programming, A project-based introduction to building efficient, safe, and readable web and CLI applications.

My Take on Crystal

Crystal is an easy-to-learn and readable programming language and may just be worth spending a little more time to pursue it further. Crystal is shaping up to be more than just a strong alternative to Ruby. From a performance perspective, Crystal is my choice for future "Ruby" projects.

Learning Crystal - Buy the Books

Books provide a structured approach to learning a "new" language and offer more in-depth information than online tutorials and training.

Crystal Programming, A project-based introduction to building efficient, safe, and readable web and CLI applications by George Dietrich and Guilherme Bernal, Packt Publishing (May 2022), 356 pages, ISBN-13: 978-1801818674, ISBN-10: 1801818673.

Programming Crystal: Create High-Performance, Safe, Concurrent Apps by Ivo Balbaert and Simon St. Laurent, Pragmatic Bookshelf; 1st edition (February 19, 2019), 246 pages, ISBN-10: 1680502867, ISBN-13: 978-1680502862

E-Books and Paperbacks are available for both books listed above. Note that I may receive a commission from Amazon for purchases made using links contained in this post.

Crystal Resources / Programming

Did you find this article valuable?

Support Redge Shepherd by becoming a sponsor. Any amount is appreciated!