文字列
- "" と '' は内部的に大きな違いがある
- UTF-8エンコーディングで文字列を保持できる
- エスケープシーケンスを含められる
#{...}
で式展開ができる\
でエスケープできる- ヒアドキュメントをサポート
ヒアドキュメント 閉じ"""のインデント位置でヒアドキュメント全体のインデント位置が決まる 閉じ"""のインデント位置より前にドキュメントがあると、閉じ"""の位置までインデントされる この時warningメッセージが表示される
"11.1.exs"
warning: outdented heredoc line. The contents inside the heredoc should be indented at the same level as the closing """. The following is forbidden:
def text do
"""
contents
"""
end
Instead make sure the contents are indented as much as the heredoc closing:
def text do
"""
contents
"""
end
The current heredoc line is indented too little
11.1.exs:30
start
my
string
end
---
start
my
string
end
---
start
my
string
end
---
start
my
string
end
---
start
my
string
end
[]
シジル(Sigil)
~
で始まるリテラルの代わりになる文法のこと
> ~C[1\n2#{1+2}]
'1\\n2\#{1+2}'
> ~c"1\n2#{1+2}"
'1\n23'
> ~S[1\n2#{1+2}]
"1\\n2\#{1+2}"
> ~s/1\n2#{1+2}/
"1\n23"
> ~W[the c#{'a'}t sat on the mat]
["the", "c\#{'a'}t", "sat", "on", "the", "mat"]
> ~w[the c#{'a'}t sat on the mat]
["the", "cat", "sat", "on", "the", "mat"]
> Regex.scan ~R[1\n2#{1+2}], "1\n2#{1+2}"
[]
> Regex.scan ~r[1\n2#{1+2}], "1\n2#{1+2}"
[["1\n23"]]
~W
~w
のオプション
a: atomのリスト
c: 文字のリスト
s: 文字列のリスト
> ~w[the c#{'a'}t sat on the mat]a
[:the, :cat, :sat, :on, :the, :mat]
> ~w[the c#{'a'}t sat on the mat]c
['the', 'cat', 'sat', 'on', 'the', 'mat']
> ~w[the c#{'a'}t sat on the mat]s
["the", "cat", "sat", "on", "the", "mat"]
> ~w{a{b}
["a{b"]
> ~w{a}b}
** (SyntaxError) iex:8: unexpected token: }
> ~w{a\}b}
["a}b"]
> ~w{a{b}}
** (SyntaxError) iex:9: unexpected token: }
> ~w{a\{b\}}
["a{b}"]
> ~w'''
> the
> c#{'a'}t
> sat
> '''
["the", "cat", "sat"]
> ~W'''
> the
> c#{'a'}t
> sat
> '''
["the", "c\#{'a'}t", "sat"]
> ~w"""
> the
> c#{'a'}t
> sat
> """a
[:the, :cat, :sat]
> ~w'''
> the
> c#{'a'}t
> sat
> '''c
['the', 'cat', 'sat']
> ~w'''
> the
> c#{'a'}t
> sat
> '''s
["the", "cat", "sat"]