Catalog / Crystal Programming Language Cheatsheet
Crystal Programming Language Cheatsheet
A concise reference for the Crystal programming language, covering syntax, data types, control flow, and common standard library features. Designed for quick lookup and efficient development.
Basics & Syntax
Basic Types
|
32-bit signed integer (default |
|
64-bit signed integer |
|
32-bit floating point number |
|
64-bit floating point number (default |
|
|
|
UTF-8 encoded character sequence |
|
Unicode code point |
Variable Declaration
|
|
|
Operators
Arithmetic |
|
Comparison |
|
Logical |
|
Bitwise |
|
Assignment |
|
Control Flow
Conditional Statements
`if condition codeend` |
`if condition codeelse codeend` |
`if condition codeelsif condition codeelse codeend` |
`unless condition codeend |
Looping
`while condition codeend` |
`until condition codeend |
`loop do codebreak if condition |
|
Case Statement
`case value codewhen condition2 codeelse codeend` |
Methods & Blocks
Method Definition
`def method_name(arg1 : Type, arg2 : Type) codereturn value |
`def method_name(arg1 : Type, arg2 : Type) : ReturnType codereturn value |
|
Methods can have default argument values: |
Blocks & Procs
Blocks are anonymous functions passed to methods. codeend` |
Procs are first-class blocks. codeend |
Lambdas are similar to procs but enforce argument arity. code} |
Classes & Modules
Class Definition
`class ClassName codeend` |
`class ClassName < SuperClass codeend` (inheritance) |
|
Instance variables: |
Module Definition
`module ModuleName codeend` |
Modules can be used for namespacing and mixins. |
|
|
Structs
|
Structs are value types (passed by value). |