Skip to content
Tutorials Center
My learning

Topics

Crystal Tutorial

Learn Crystal step by step: 37 lessons in 9 chapters with examples, quizzes, diagrams and projects.

Start learning →

of lessons completed

Getting Started

  1. 1 What Is Crystal? Meet Crystal, a compiled language with Ruby-inspired syntax, static types, type inference and native speed. Learn what it is good for and how this course works.
  2. 2 Installing Crystal and Your First Program Install the Crystal compiler on Linux, macOS or Windows, then write, run and build your first program with crystal run and crystal build, including how to read compiler errors.
  3. 3 The Compiler and Toolchain Tour the Crystal command-line tools (crystal init, build, run, tool format, spec, docs and shards). Learn project layout, flags and a daily development workflow.
  4. 4 How Crystal Compiles and Infers Types Understand how the Crystal compiler infers types across your whole program, creates one specialised copy of a method per type, and why this gives both short code and speed.

Syntax and Types

  1. 5 Variables and Constants Learn how to create Crystal variables and constants, how type inference tracks them, why reassigning with another type creates a union, and the naming conventions to follow.
  2. 6 Numbers and Math Learn Crystal numeric types (Int8 to Int128, UInt, Float32, Float64), literals, division rules, overflow behaviour, conversions and the Math module, with read-only code and runnable Ruby stand-ins.
  3. 7 Strings, Chars and Symbols Work with Crystal text, from immutable UTF-8 strings, Char literals, symbols, interpolation, heredocs, common string methods and String.build, with read-only code and Ruby stand-ins.
  4. 8 Union Types and nil Safety Learn how Crystal union types and the Nil type work, how the compiler narrows types with if, is_a?, case and try, and how to avoid null errors completely.
  5. 9 Type Restrictions, Casting and Type Checks Learn how to add type restrictions to Crystal method parameters, check types with is_a? and responds_to?, cast with as and as?, and use typeof, alias and type aliases safely.

Control Flow and Methods

  1. 10 Conditionals and case Control program flow in Crystal with if, elsif, unless, the ternary operator, case/when with ranges and types, case/in pattern matching and truthiness rules, with Ruby stand-ins.
  2. 11 Loops, Ranges and Iteration Repeat code in Crystal with while, until, loop, times, upto, step and each, plus inclusive and exclusive ranges, break, next, and the idiomatic iterator style with Ruby stand-ins.
  3. 12 Methods and Arguments Define Crystal methods with default values, named arguments, splats, double splats, return types, the implicit return and the short one-line form, with Ruby stand-ins you can run.
  4. 13 Overloading, Return Types and Procs Learn how Crystal overloads methods by argument types and count, how return types are inferred and checked, and how to create, store and call procs and closures.
  5. 14 Exceptions and Error Handling Handle errors in Crystal with begin, rescue, else, ensure and raise, create custom exception classes, and choose between exceptions, nil returns and the ? method convention.

Classes and Modules

  1. 15 Classes, Properties and Initialization Create Crystal classes with initialize, instance variables, getter, setter and property macros, class methods and class variables, to_s and inspect, with Ruby stand-ins you can run.
  2. 16 Inheritance and Abstract Classes Learn Crystal inheritance with the < operator, super, method overriding, abstract classes and methods, protected and private visibility, virtual types and polymorphism with Ruby stand-ins.
  3. 17 Structs, Records and Value Types Learn the difference between Crystal classes and structs, how value semantics work, and when to use struct, the record macro, Tuple and NamedTuple for fast, lightweight data.
  4. 18 Modules and Mixins Use Crystal modules as namespaces and as mixins with include and extend, add shared behaviour such as Comparable and Enumerable to your own classes, and organise code across files.
  5. 19 Generics and Enums Write reusable typed code with Crystal generic classes and methods, then model fixed sets of choices with enums, enum methods, values and @[Flags] enums, using Ruby stand-ins you can run.

Collections and Blocks

  1. 20 Arrays and Slices in Crystal Learn Crystal arrays: typed element types, indexing, push and pop, ranges and slicing, nil-safe access, Slice and StaticArray, with runnable Ruby stand-ins.
  2. 21 Hashes, Tuples and Named Tuples in Crystal Learn Crystal Hash, Set, Tuple and NamedTuple: typed keys and values, default values, fetch and nil-safe lookup, iteration, destructuring, with Ruby stand-ins.
  3. 22 Blocks, Procs and yield in Crystal Learn Crystal blocks, yield, block arguments, &block capture, Proc literals, closures, return and next inside blocks, and inlining, with Ruby stand-ins.
  4. 23 Enumerable, Iterators and Lazy Pipelines in Crystal Learn Crystal Enumerable and Iterator: map, select, reduce, group_by, each_slice, zip, lazy iterator chains, and how to make your own class enumerable.

Macros

  1. 24 Macro Basics in Crystal Learn Crystal macros: what compile-time code generation is, macro definitions, {{ }} output, {% %} control, macro arguments as AST nodes, and Ruby stand-ins.
  2. 25 Macro Reflection in Crystal โ€“ Inspecting Types at Compile Time Learn how Crystal macros look inside types with @type, instance_vars, methods and annotations to generate to_h, equality and JSON-like code, with Ruby stand-ins.
  3. 26 Annotations and Compile-Time Flags in Crystal Learn Crystal annotations (@[Deprecated], @[AlwaysInline], custom annotations), compile flags with flag?, platform checks and compile-time constants, with Ruby stand-ins.

Concurrency and Fibers

  1. 27 Fibers and spawn in Crystal Understand Crystal concurrency: lightweight fibers, spawn, the cooperative scheduler, sleep and Fiber.yield, why main exiting stops everything, with Ruby stand-ins.
  2. 28 Channels in Crystal โ€“ Talking Between Fibers Learn Crystal channels: Channel(T), send and receive, buffered channels, close and Channel::ClosedError, producer-consumer pipelines and fan-in, with Ruby stand-ins.
  3. 29 select, Mutex and Parallelism in Crystal Learn Crystal select over several channels, timeouts, Mutex, Atomic counters, race conditions and when fibers run in parallel with preview_mt, with Ruby stand-ins.

Shards and Web

  1. 30 Shards and Dependencies in Crystal Learn Crystal's package manager Shards: shard.yml, dependencies, version constraints, shard.lock, install and update, requiring libraries and building a shard, with Ruby stand-ins.
  2. 31 Building an HTTP Server in Crystal Build web servers in Crystal with HTTP::Server, request and response contexts, handler chains, simple routing, status codes, plus a look at Kemal, with Ruby stand-ins.
  3. 32 JSON and YAML in Crystal Work with JSON and YAML in Crystal: JSON.parse and JSON::Any, to_json, JSON::Serializable with field options, YAML, error handling and Ruby stand-ins.
  4. 33 Testing in Crystal with Spec Learn testing in Crystal with the built-in Spec framework: describe, it, should and expect, hooks, pending tests, running crystal spec, and testing macros and fibers, with a Ruby mini-spec stand-in.

Projects and Practice

  1. 34 Project: A To-Do Command-Line App in Crystal Build a command-line to-do app in Crystal step by step: structs, JSON storage, OptionParser, subcommands, errors and specs, with a runnable Ruby version of the logic.
  2. 35 Project: A Tiny Router and URL Shortener in Crystal Build a small URL shortener in Crystal: a path router with parameters, base62 ids, JSON API, redirects and validation, with a runnable Ruby model of the logic.
  3. 36 Project: A Worker Pool with Channels in Crystal Build a concurrent worker pool in Crystal with fibers and channels: jobs, results, graceful shutdown, errors, timeouts and a statistics owner fiber, with Ruby models.
  4. 37 Practice: Crystal Exercises with Solutions Practice Crystal with eight exercises on collections, blocks, macros, channels and JSON, each with a hint, a Crystal solution and a runnable Ruby version to experiment with.