Literals

Crystal provides several literals for creating values of some basic types.

LiteralSample values
Nilnil
Booltrue, false
Integers18, -12, 19_i64, 14_u32,64_u8
Floats1.0, 1.0_f32, 1e10, -0.5
Char‘a’, ‘\n’, ‘あ’
String“foo\tbar”, %(“あ”), %q(foo #{foo})
Symbol:symbol, :”foo bar”
Array[1, 2, 3], [1, 2, 3] of Int32, %w(one two three)
Array-likeSet{1, 2, 3}
Hash{“foo” => 2}, {} of String => Int32
Hash-likeMyType{“foo” => “bar”}
Range1..9, 1…10, 0..var
Regex/(foo)?bar/, /foo #{foo}/imx, %r(foo/)
Tuple{1, “hello”, ‘x’}
NamedTuple{name: “Crystal”, year: 2011}, {“this is a key”: 1}
Proc->(x : Int32, y : Int32) { x + y }
Commandecho foo, %x(echo foo)