リテラル¶
Crystal には、数多くの基本的な型に対応したリテラルが用意されています。
| リテラル | 値のサンプル |
|---|---|
| Nil | nil |
| 真偽値 (Bool) | true, false |
| 整数 (Integer) | 18, -12, 19_i64, 14_u32,64_u8 |
| 浮動小数点数 (Float) | 1.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) |
| 配列ライク | Set{1, 2, 3} |
| ハッシュ (Hash) | {"foo" => 2}, {} of String => Int32 |
| Hashライク | MyType{"foo" => "bar"} |
| 範囲 (Range) | 1..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 } |
| コマンド (Command) | `echo foo`, %x(echo foo) |