You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We have already learned Elixir provides double-quoted strings and single-quoted char lists. However, this only covers the surface of structures that have textual representation in the language. Atoms are, for example, another structure which are mostly created via the `:atom` representation.
One of Elixir's goals is extensibility: developers should be able to extend the language to particular domains. Computer science has become such a wide field that it is impossible for a language to tackle many fields as part of its core. Our best bet is to rather make the language extensible, so developers, companies and communities can extend the language to their relevant domains.
14
18
19
+
この章では,言語によって提供されている機能で,文字表現として動作するシギルについて調べていきます.
20
+
15
21
In the chapter, we are going to explore sigils, which are one of the mechanisms provided by the language for working with textual representations.
Sigils start with the tilde (`~`) character which is followed by a letter and then a separator. The most common sigil in Elixir is `~r` for [regular expressions](https://en.wikipedia.org/wiki/Regular_Expressions):
Elixir provides Perl-compatible regular expressions (regexes), as implemented by the [PCRE](http://www.pcre.org/) library. Regexes also support modifiers. For example, the `i` modifier makes a regular expression case insensitive:
Check out the [`Regex` module](/docs/stable/elixir/Regex.html) for more information on other modifiers and the supported operations with regular expressions.
The reasoning in supporting different operators is that different separators can be more convenient to different sigils. For example, using parentheses for regular expressions may be a confusing choice as they can get mixed with the parentheses inside the regex. However, parentheses can be handy for other sigils, as we will see in the next section.
56
72
57
-
## 19.2 Strings, char lists and words sigils
73
+
## 19.2 文字列,文字リストして語句のシギル - Strings, char lists and words sigils
74
+
75
+
正規表現に加えて,Elixirはその他に3つのシギルを提供しています.
58
76
59
77
Besides regular expressions, Elixir ships with three other sigils.
60
78
79
+
`~s`シギルは文字列を生成するのに使われ,ダブルクォートするのに似ています:
80
+
61
81
The `~s` sigil is used to generate strings, similar to double quotes:
62
82
63
83
```iex
64
84
iex> ~s(this is a string with "quotes")
65
85
"this is a string with \"quotes\""
66
86
```
67
87
88
+
`~c`シギルは文字リストを生成するのに使われます:
89
+
68
90
While `~c` is used to generate char lists:
69
91
70
92
```iex
71
93
iex> ~c(this is a string with "quotes")
72
94
'this is a string with "quotes"'
73
95
```
74
96
97
+
`~w`シギルはスペースで区切られた語句からリストを生成するのに使われます:
98
+
75
99
The `~w` sigil is used to generate a list of words separated by white space:
Besides lowercase sigils, Elixir supports uppercase sigils. While both `~s` and `~S` will return strings, the first one allows escape codes and interpolation while the second does not:
90
118
91
119
```elixir
@@ -95,8 +123,27 @@ iex> ~S(String without escape codes and without #{interpolation})
95
123
"String without escape codes and without \#{interpolation}"
96
124
```
97
125
126
+
以下のエスケープコードは文字列と文字リストで使うことができます:
127
+
98
128
The following escape codes can be used in strings and char lists:
The most common case for heredoc sigils is when writing documentation. For example, if you need to write escape characters in your documentation, it can become error prone as we would need to double-escape some characters:
127
178
128
179
```elixir
@@ -138,6 +189,8 @@ Converts double-quotes to single-quotes.
138
189
defconvert(...)
139
190
```
140
191
192
+
しかし`~S`を使えば,問題を完全に避けられます:
193
+
141
194
By using using `~S`, we can avoid this problem altogether:
142
195
143
196
```elixir
@@ -153,7 +206,9 @@ Converts double-quotes to single-quotes.
As hinted at the beginning of this chapter, sigils inElixir are extensible. In fact, the sigil `~r/foo/i` is equivalent to calling the `sigil_r` function with two arguments:
We can also provide our own sigils by simply implementing the proper function. For example, let's implement the `~i(13)` sigil that returns an integer:
Sigils can also be used to do compile-time work with the help of macros. For example, regular expressions in Elixir are compiled into efficient representation during compilation of the source code, therefore skipping this step at runtime. If you have interest in the subject, we recommend you to learn more about macros and check how those sigils are implemented in the `Kernel` module.
0 commit comments