Crystal is a programming language made for humans to write easily, but built for computers to run incredibly fast.
In the past, programmers had to make a tough choice. You could only pick one of these two options:
Languages like Ruby or Python are very fun and easy to read. But when the computer runs them, they can be a bit slow.
Languages like C or C++ are lightning fast. But writing code in them is very difficult and takes a lot of extra words.
Crystal gives you both! It takes the fun, easy look of Ruby, and gives it the blazing fast speed of C.
Crystal is a compiled language. This means it translates your work early.
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.
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.
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.
// You have to tell the computer exactly what everything is.
String name = "Sarah";
Integer age = 30;
Boolean is_happy = true;
# 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.
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.
Because it is so fast, Crystal can handle thousands of users on a website at the exact same time without breaking a sweat.
If you need a tool that runs in your command line, Crystal starts up instantly. No waiting around.
When you need to process millions of files or do heavy math on your computer, Crystal's C-like speed does the heavy lifting.
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
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.
In Crystal, these helpful packages of code are called Shards.
You write a simple list of Shards you want in a file called `shard.yml`.
The tool downloads them instantly, and you can use them in your app!
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!
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".
"Here is your box. Good luck! *CRASH* Oh, sorry, it was empty."
"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."
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!
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.
Math logic works
Users can log in properly
Database is safe and secure
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."
def hello()
puts"hi"
end
def hello() puts "hi" end
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.
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.
Tiny, simple, and incredibly fast. Perfect if you want to build a small website or tool with only a few lines of code.
A giant, powerful toolkit. Perfect if you want to build a massive web application like Facebook or Twitter with user accounts and databases.
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!
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.
Learning alone is hard. Crystal has a very friendly village of people online who love to help beginners learn the ropes.
Download the Crystal compiler to your computer. It is free and open for everyone.
Open any text editor. Write your friendly, Ruby-like code and save it as `app.cr`.
Type `crystal run app.cr` in your terminal and watch it fly!