Crystal Cheat Sheet
Core Crystal syntax covering static typing, classes, collections, and blocks for a Ruby-like compiled language.
2 PagesIntermediateApr 12, 2026
Basics
Variables, interpolation, and methods.
crystal
# Variables and printingname = "World"puts "Hello, #{name}!" # string interpolationx = 42 # type inferred as Int32def add(a, b) a + bendputs add(2, 3)
Static Types
Explicit types, nilable types, and unions.
crystal
x : Int32 = 42y : Float64 = 3.14s : String = "text"n : Int32? = nil # nilable type (union with Nil)def describe(x : Int32 | String) case x when Int32 puts "int: #{x}" when String puts "string: #{x}" endend
Classes
Inheritance and instance variables.
crystal
class Animal property name : String def initialize(@name : String) end def speak "#{name} makes a sound" endendclass Dog < Animal def speak "#{name} barks" endendputs Dog.new("Rex").speak
Collections
Arrays and hashes.
- [1, 2, 3]- Array(Int32) literal
- {"a" => 1, "b" => 2}- Hash(String, Int32) literal
- arr.map { |x| x * 2 }- Transforms each element
- arr.select { |x| x > 1 }- Filters elements matching a condition
- arr.each { |x| puts x }- Iterates over elements
- arr << 4- Appends an element (alias for push)
Blocks & Procs
Yielding and typed procs.
- def each(&block)- Method that accepts a block argument
- yield x- Invokes the block passed to the current method
- ->(x : Int32) { x * 2 }- Typed Proc literal
- arr.each_with_index { |x, i| }- Iterates with an index
- 1.upto(5) { |i| }- Iterates an inclusive numeric range
- String.build { |s| s << "x" }- Efficiently builds a string
Pro Tip
Crystal's type inference is compile-time and static despite Ruby-like syntax — build with crystal build --release for optimized binaries, since debug builds are noticeably slower.
Was this cheat sheet helpful?
Explore Topics
#Crystal#CrystalCheatSheet#Programming#Intermediate#StaticTypes#Classes#Collections#BlocksProcs#OOP#DataStructures#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance