跳至內容

字面值

Crystal 提供了數個字面值來建立一些基本類型的值。

字面值 範例值
Nil nil
布林值 truefalse
整數 18-1219_i6414_u3264_u8
浮點數 1.01.0_f321e10-0.5
字元 'a''\n''あ'
字串 "foo\tbar"%("あ")%q(foo #{foo})
符號 :symbol:"foo bar"
陣列 [1, 2, 3][1, 2, 3] of Int32%w(one two three)
類陣列 Set{1, 2, 3}
雜湊 {"foo" => 2}{} of String => Int32
類雜湊 MyType{"foo" => "bar"}
範圍 1..91...100..var
正規表達式 /(foo)?bar//foo #{foo}/imx%r(foo/)
元組 {1, "hello", 'x'}
具名元組 {name: "Crystal", year: 2011}{"this is a key": 1}
Proc ->(x : Int32, y : Int32) { x + y }
命令 `echo foo`%x(echo foo)