getbetterat.work
TOPIC: CRYSTAL_LANGUAGE

Read like Ruby.
Run like C.

Crystal is a programming language made for humans to write easily, but built for computers to run incredibly fast.

Fast
Safe
Friendly

The Big Trade-Off

In the past, programmers had to make a tough choice. You could only pick one of these two options:

Easy to Write, but Slow

Languages like Ruby or Python are very fun and easy to read. But when the computer runs them, they can be a bit slow.

Super Fast, but Hard

Languages like C or C++ are lightning fast. But writing code in them is very difficult and takes a lot of extra words.

The Crystal Solution

Crystal gives you both! It takes the fun, easy look of Ruby, and gives it the blazing fast speed of C.

Why is it so fast?

Crystal is a compiled language. This means it translates your work early.

How other languages do it:

They read your code line-by-line while the program is running. This is like translating a book word-for-word while reading it out loud. It takes time.

How Crystal does it (Compilation):

Crystal reads all your code at once, translates it all into computer-native language (machine code), and saves it. When you run it, it just sprints to the finish line without stopping to think. It is already fully translated.

Speed Comparison (Approximate)

Ruby (Interpreted) Slower
Crystal (Compiled) Blazing Fast
C (Compiled) Maximum Speed

Smart Guessing (Type Inference)

Computers need to know what kind of data they are holding. Is it a number? Is it a word? Most fast languages force you to write this out every single time.

THE OLD WAY (Lots of typing)

// You have to tell the computer exactly what everything is.

String name = "Sarah";

Integer age = 30;

Boolean is_happy = true;

THE CRYSTAL WAY (Smart)

# Crystal looks at the value and guesses the type perfectly.

name = "Sarah"

age = 30

is_happy = true

This is called Type Inference. You type less, but the computer still knows exactly what the data is.

No More "Oops" Errors

Because Crystal figures out all your data types before the program even runs, it acts like a giant safety net. If you try to add a word and a number together, Crystal will stop you and say:

"Hey! You can't do that!"

This means bugs are caught on your screen, not when your user is trying to use your app. This is called Static Typing.

What is it good for?

Web Applications

Because it is so fast, Crystal can handle thousands of users on a website at the exact same time without breaking a sweat.

Terminal Tools

If you need a tool that runs in your command line, Crystal starts up instantly. No waiting around.

System Utilities

When you need to process millions of files or do heavy math on your computer, Crystal's C-like speed does the heavy lifting.

Look how simple it is:

class Greeter
  def initialize(name)
    @name = name
  end

  def salute
    puts "Hello, #{@name}! Crystal is easy."
  end
end

my_greeter = Greeter.new("World")
my_greeter.salute

Adding Superpowers with "Shards"

When you want your code to do something special (like talk to a database, or read a PDF), you don't have to build it from scratch. Other programmers have already built these tools for you.

1

In Crystal, these helpful packages of code are called Shards.

2

You write a simple list of Shards you want in a file called `shard.yml`.

3

The tool downloads them instantly, and you can use them in your app!

Doing Many Things at Once

Imagine cooking dinner. You don't wait for the water to boil before you start cutting carrots. You do both at the same time.

Crystal uses something called Fibers. Fibers are like tiny, super-lightweight workers inside your computer.

Worker 1

Downloading a picture

Worker 2

Saving to database

Worker 3

Sending an email

They all work together at the exact same time without slowing down your main program!

The "Empty Box" Problem

In many older languages, if you ask for a box of data and the box is accidentally empty (called "Null"), your whole program crashes instantly. People call this the "Billion Dollar Mistake".

Other Languages:

"Here is your box. Good luck! *CRASH* Oh, sorry, it was empty."

Crystal:

"Wait! That box might be empty. You must write rules for what to do if it is empty before I let you run the code."

Code That Writes Code

Sometimes, programming is boring because you type the exact same things over and over. Crystal fixes this with Macros.

Think of a Macro as a tiny robot inside your code. You tell the robot the rules once, and it types out hundreds of lines of code for you automatically before the program even starts!

# You write 1 tiny line of macro...

generate_rules_for(["name", "age", "city"])

# The robot secretly writes 50 lines of code for you!

Checking Your Homework

How do you know your code actually works? You write tests! In other languages, you have to download 5 different heavy tools just to test your code. Crystal includes an automatic homework-checker right out of the box.

> crystal spec

Math logic works

Users can log in properly

Database is safe and secure

The Tidy Desk Rule

Programmers often argue about how code should look. Should we use two spaces? Four spaces? Crystal says: "Stop arguing, I will organize it for you perfectly every time."

Messy Code

def    hello()
  puts"hi"
    end

After Crystal organizes it

def hello()
  puts "hi"
end

The Free Toolbelt

When you install Crystal, it comes with a giant toolbox called the "Standard Library". You don't have to search the internet and download extra files to do basic, everyday computer tasks.

Read JSON Data
Talk to the Web
Read/Write Files
Complex Math

Website Starter Kits

If you want to build a website, the Crystal community has already built amazing "starter kits" (called Frameworks) for you so you don't start from scratch.

Kemal

Tiny, simple, and incredibly fast. Perfect if you want to build a small website or tool with only a few lines of code.

Amber

A giant, powerful toolkit. Perfect if you want to build a massive web application like Facebook or Twitter with user accounts and databases.

Everything is a Building Block

In Crystal, everything is considered an "Object" (like a smart Lego block). Even simple numbers are smart blocks that can do tricks.

Instead of doing math on a number, you ask the number to do things:

5.times { puts "Hello!" }

# This prints "Hello!" exactly 5 times. The number 5 is smart enough to count itself!

Talking to the Old Masters (C)

The 'C' programming language has been around for 50 years. It has thousands of tools to do almost anything. Crystal has a special bridge that lets you plug into those 50-year-old C tools perfectly, without having to write hard C code yourself.

Crystal Code
Old C Tools

The Crystal Village

Learning alone is hard. Crystal has a very friendly village of people online who love to help beginners learn the ropes.

Discord Chat
Crystal Forum
Official Guide

Your First 3 Steps

1.

Install

Download the Crystal compiler to your computer. It is free and open for everyone.

2.

Write

Open any text editor. Write your friendly, Ruby-like code and save it as `app.cr`.

3.

Run

Type `crystal run app.cr` in your terminal and watch it fly!

The Good Stuff

  • Extremely fast. Saves server costs.
  • Beautiful, clean syntax. Easy to read.
  • Catches errors before the program runs.

Things to know

  • Smaller community than giants like Python or JavaScript.
  • Takes a few seconds to compile (translate) before it runs.
  • Windows support is still getting better.