-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfile.literals.html
433 lines (297 loc) · 25.8 KB
/
file.literals.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0, user-scalable=no'>
<meta name='apple-touch-fullscreen' content='yes'>
<meta name='apple-mobile-web-app-capable' content='yes'>
<meta name='apple-mobile-web-app-status-bar-style' content='rgba(228,228,228,1.0)'>
<title>File: Literals — Ruby-2.6.10</title>
<link rel='stylesheet' type='text/css' href='../css/y_fonts.css' />
<link rel='stylesheet' type='text/css' href='../css/highlight.github.css' />
<link rel='stylesheet' type='text/css' href='../css/y_style.css' />
<link rel='stylesheet' type='text/css' href='../css/y_list.css' />
<link rel='stylesheet' type='text/css' href='../css/y_color.css' />
<script type='text/javascript'>
var pathId = "literals",
relpath = '';
var t2Info = {
CSEP: '.',
ISEP: '#',
NSEP: '::'
};
</script>
<script type='text/javascript' charset='utf-8' src='../js/highlight.pack.js'></script>
<script type='text/javascript' charset='utf-8' src='../js/y_app.js'></script>
</head>
<body>
<svg id='y_wait' class viewBox='0 0 90 90'></svg>
<div id='settings' class='hidden'></div>
<div id='y_list' class='d h'>
<header id='list_header'></header>
<nav id= 'list_nav' class='y_nav l_nav'>
<ul id='list_items'></ul>
</nav>
</div>
<div id='y_toc' class='f h'>
<header id='toc_header'></header>
<nav id= 'toc_nav' class='y_nav t_nav'>
<ol id='toc_items'></ol>
</nav>
</div>
<div id='y_main' tabindex='-1'>
<header id='y_header'>
<div id='y_menu'>
<a id='home_no_xhr' href='/'>Home</a> »
<a href='.'>Ruby-2.6.10</a> »
<a href='_index.html'>Index</a> »
<span class='title'><a id='t2_doc_top' href='#'>File: Literals ▲</a></span>
</div>
<a id='list_href' href="file_list.html"></a>
<div id='y_measure_em' class='y_measure'></div>
<div id='y_measure_vh' class='y_measure'></div>
<span id='y_measure_50pre' class='y_measure'><code>123456789_123456789_123456789_123456789_123456789_</code></span>
</header>
<div id='content' class='file'>
<h1 id="label-Literals">Literals</h1>
<p>Literals create objects you can use in your program. Literals include:</p>
<ul><li>
<p>Booleans and nil</p>
</li><li>
<p>Numbers</p>
</li><li>
<p>Strings</p>
</li><li>
<p>Symbols</p>
</li><li>
<p>Arrays</p>
</li><li>
<p>Hashes</p>
</li><li>
<p>Ranges</p>
</li><li>
<p>Regular Expressions</p>
</li><li>
<p>Procs</p>
</li></ul>
<h2 id="label-Booleans+and+nil">Booleans and nil</h2>
<p><code>nil</code> and <code>false</code> are both false values. <code>nil</code> is sometimes used to indicate “no value” or “unknown” but evaluates to <code>false</code> in conditional expressions.</p>
<p><code>true</code> is a true value. All objects except <code>nil</code> and <code>false</code> evaluate to a true value in conditional expressions.</p>
<p>(There are also the constants <code>TRUE</code>, <code>FALSE</code> and <code>NIL</code>, but the lowercase literal forms are preferred.)</p>
<h2 id="label-Numbers">Numbers</h2>
<p>You can write integers of any size as follows:</p>
<pre class="code ruby"><code class="ruby"><span class='int'>1234</span>
<span class='int'>1_234</span></code></pre>
<p>These numbers have the same value, 1,234. The underscore may be used to enhance readability for humans. You may place an underscore anywhere in the number.</p>
<p>Floating point numbers may be written as follows:</p>
<pre class="code ruby"><code class="ruby"><span class='float'>12.34</span>
<span class='float'>1234e-2</span>
<span class='float'>1.234E1</span></code></pre>
<p>These numbers have the same value, 12.34. You may use underscores in floating point numbers as well.</p>
<p>You can use a special prefix to write numbers in decimal, hexadecimal, octal or binary formats. For decimal numbers use a prefix of <code>0d</code>, for hexadecimal numbers use a prefix of <code>0x</code>, for octal numbers use a prefix of <code>0</code> or <code>0o</code>, for binary numbers use a prefix of <code>0b</code>. The alphabetic component of the number is not case-sensitive.</p>
<p>Examples:</p>
<pre class="code ruby"><code class="ruby"><span class='int'>0d170</span>
<span class='int'>0D170</span>
<span class='int'>0xaa</span>
<span class='int'>0xAa</span>
<span class='int'>0xAA</span>
<span class='int'>0Xaa</span>
<span class='int'>0XAa</span>
<span class='int'>0XaA</span>
<span class='int'>0252</span>
<span class='int'>0o252</span>
<span class='int'>0O252</span>
<span class='int'>0b10101010</span>
<span class='int'>0B10101010</span></code></pre>
<p>All these numbers have the same decimal value, 170. Like integers and floats you may use an underscore for readability.</p>
<h3 id="label-Rational+numbers">Rational numbers</h3>
<p>Numbers suffixed by <code>r</code> are Rational numbers.</p>
<pre class="code ruby"><code class="ruby"><span class='rational'>12r</span> <span class='comment'>#=> (12/1)
</span><span class='rational'>12.3r</span> <span class='comment'>#=> (123/10)</span></code></pre>
<p>Rational numbers are exact, whereas Float numbers are inexact.</p>
<pre class="code ruby"><code class="ruby"><span class='rational'>0.1r</span> <span class='op'>+</span> <span class='rational'>0.2r</span> <span class='comment'>#=> (3/10)
</span><span class='float'>0.1</span> <span class='op'>+</span> <span class='float'>0.2</span> <span class='comment'>#=> 0.30000000000000004</span></code></pre>
<h3 id="label-Complex+numbers">Complex numbers</h3>
<p>Numbers suffixed by <code>i</code> are Complex (or imaginary) numbers.</p>
<pre class="code ruby"><code class="ruby"><span class='imaginary'>1i</span> <span class='comment'>#=> (0+1i)
</span><span class='imaginary'>1i</span> <span class='op'>*</span> <span class='imaginary'>1i</span> <span class='comment'>#=> (-1+0i)</span></code></pre>
<p>Also Rational numbers may be imaginary numbers.</p>
<pre class="code ruby"><code class="ruby"><span class='imaginary'>12.3ri</span> <span class='comment'>#=> (0+(123/10)*i)</span></code></pre>
<p><code>i</code> must be placed after <code>r</code>, the opposite is not allowed.</p>
<pre class="code ruby"><code class="ruby">12.3ir #=> syntax error</code></pre>
<h2 id="label-Strings">Strings</h2>
<p>The most common way of writing strings is using <code>"</code>:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>This is a string.</span><span class='tstring_end'>"</span></span></code></pre>
<p>The string may be many lines long.</p>
<p>Any internal <code>"</code> must be escaped:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>This string has a quote: \". As you can see, it is escaped</span><span class='tstring_end'>"</span></span></code></pre>
<p>Double-quote strings allow escaped characters such as <code>\n</code> for newline, <code>\t</code> for tab, etc. The full list of supported escape sequences are as follows:</p>
<pre class="code ruby"><code class="ruby">\a bell, ASCII 07h (BEL)
\b backspace, ASCII 08h (BS)
\t horizontal tab, ASCII 09h (TAB)
\n newline (line feed), ASCII 0Ah (LF)
\v vertical tab, ASCII 0Bh (VT)
\f form feed, ASCII 0Ch (FF)
\r carriage return, ASCII 0Dh (CR)
\e escape, ASCII 1Bh (ESC)
\s space, ASCII 20h (SPC)
\\ backslash, \
\nnn octal bit pattern, where nnn is 1-3 octal digits ([0-7])
\xnn hexadecimal bit pattern, where nn is 1-2 hexadecimal digits ([0-9a-fA-F])
\unnnn Unicode character, where nnnn is exactly 4 hexadecimal digits ([0-9a-fA-F])
\u{nnnn ...} Unicode character(s), where each nnnn is 1-6 hexadecimal digits ([0-9a-fA-F])
\cx or \C-x control character, where x is an ASCII printable character
\M-x meta character, where x is an ASCII printable character
\M-\C-x meta control character, where x is an ASCII printable character
\M-\cx same as above
\c\M-x same as above
\c? or \C-? delete, ASCII 7Fh (DEL)</code></pre>
<p>Any other character following a backslash is interpreted as the character itself.</p>
<p>Double-quote strings allow interpolation of other values using <code>#{...}</code>:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>One plus one is two: </span><span class='embexpr_beg'>#{</span><span class='int'>1</span> <span class='op'>+</span> <span class='int'>1</span><span class='embexpr_end'>}</span><span class='tstring_end'>"</span></span></code></pre>
<p>Any expression may be placed inside the interpolated section, but it’s best to keep the expression small for readability.</p>
<p>Interpolation may be disabled by escaping the “#” character or using single-quote strings:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>#{1 + 1}</span><span class='tstring_end'>'</span></span> <span class='comment'>#=> "\#{1 + 1}"</span></code></pre>
<p>In addition to disabling interpolation, single-quoted strings also disable all escape sequences except for the single-quote (<code>\'</code>) and backslash (<code>\\</code>).</p>
<p>You may also create strings using <code>%</code>:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='tstring_beg'>%(</span><span class='tstring_content'>1 + 1 is </span><span class='embexpr_beg'>#{</span><span class='int'>1</span> <span class='op'>+</span> <span class='int'>1</span><span class='embexpr_end'>}</span><span class='tstring_end'>)</span></span> <span class='comment'>#=> "1 + 1 is 2"</span></code></pre>
<p>There are two different types of <code>%</code> strings <code>%q(...)</code> behaves like a single-quote string (no interpolation or character escaping), while <code>%Q</code> behaves as a double-quote string. See Percent Strings below for more discussion of the syntax of percent strings.</p>
<p>Adjacent string literals are automatically concatenated by the interpreter:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>con</span><span class='tstring_end'>"</span></span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>cat</span><span class='tstring_end'>"</span></span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>en</span><span class='tstring_end'>"</span></span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>at</span><span class='tstring_end'>"</span></span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>ion</span><span class='tstring_end'>"</span></span> <span class='comment'>#=> "concatenation"
</span><span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>This string contains </span><span class='tstring_end'>"</span></span>\
<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>no newlines.</span><span class='tstring_end'>"</span></span> <span class='comment'>#=> "This string contains no newlines."</span></code></pre>
<p>Any combination of adjacent single-quote, double-quote, percent strings will be concatenated as long as a percent-string is not last.</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='tstring_beg'>%q{</span><span class='tstring_content'>a</span><span class='tstring_end'>}</span></span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>b</span><span class='tstring_end'>'</span></span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>c</span><span class='tstring_end'>"</span></span> <span class='comment'>#=> "abc"
</span><span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>a</span><span class='tstring_end'>"</span></span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>b</span><span class='tstring_end'>'</span></span> <span class='op'>%</span><span class='id identifier rubyid_q'>q</span>{<span class='id identifier rubyid_c'>c</span>} <span class='comment'>#=> NameError: uninitialized constant q</span></code></pre>
<p>There is also a character literal notation to represent single character strings, which syntax is a question mark (<code>?</code>) followed by a single character or escape sequence that corresponds to a single codepoint in the script encoding:</p>
<pre class="code ruby"><code class="ruby">?a #=> "a"
?abc #=> SyntaxError
?\n #=> "\n"
?\s #=> " "
?\\ #=> "\\"
?\u{41} #=> "A"
?\C-a #=> "\x01"
?\M-a #=> "\xE1"
?\M-\C-a #=> "\x81"
?\C-\M-a #=> "\x81", same as above
?あ #=> "あ"</code></pre>
<h3 id="label-Here+Documents">Here Documents</h3>
<p>If you are writing a large block of text you may use a “here document” or “heredoc”:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_expected_result'>expected_result</span> <span class='op'>=</span> <span class='heredoc_beg'><<HEREDOC</span>
<span class='tstring_content'>This would contain specially formatted text.
That might span many lines
</span><span class='heredoc_end'>HEREDOC</span></code></pre>
<p>The heredoc starts on the line following <code><<HEREDOC</code> and ends with the next line that starts with <code>HEREDOC</code>. The result includes the ending newline.</p>
<p>You may use any identifier with a heredoc, but all-uppercase identifiers are typically used.</p>
<p>You may indent the ending identifier if you place a “-” after <code><<</code>:</p>
<pre class="code ruby"><code class="ruby"> <span class='id identifier rubyid_expected_result'>expected_result</span> <span class='op'>=</span> <span class='heredoc_beg'><<-INDENTED_HEREDOC</span>
<span class='tstring_content'>This would contain specially formatted text.
That might span many lines
</span><span class='heredoc_end'> INDENTED_HEREDOC</span></code></pre>
<p>Note that the while the closing identifier may be indented, the content is always treated as if it is flush left. If you indent the content those spaces will appear in the output.</p>
<p>To have indented content as well as an indented closing identifier, you can use a “squiggly” heredoc, which uses a “~” instead of a “-” after <code><<</code>:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_expected_result'>expected_result</span> <span class='op'>=</span> <span class='heredoc_beg'><<~SQUIGGLY_HEREDOC</span>
<span class='ignored_sp'> </span><span class='tstring_content'>This would contain specially formatted text.
</span><span class='tstring_content'>
</span><span class='ignored_sp'> </span><span class='tstring_content'>That might span many lines
</span><span class='heredoc_end'>SQUIGGLY_HEREDOC</span></code></pre>
<p>The indentation of the least-indented line will be removed from each line of the content. Note that empty lines and lines consisting solely of literal tabs and spaces will be ignored for the purposes of determining indentation, but escaped tabs and spaces are considered non-indentation characters.</p>
<p>A heredoc allows interpolation and escaped characters. You may disable interpolation and escaping by surrounding the opening identifier with single quotes:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_expected_result'>expected_result</span> <span class='op'>=</span> <span class='heredoc_beg'><<-'EXPECTED'</span>
<span class='tstring_content'>One plus one is #{1 + 1}
</span><span class='heredoc_end'>EXPECTED
</span>
<span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_expected_result'>expected_result</span> <span class='comment'># prints: "One plus one is \#{1 + 1}\n"</span></code></pre>
<p>The identifier may also be surrounded with double quotes (which is the same as no quotes) or with backticks. When surrounded by backticks the HEREDOC behaves like Kernel#‘:</p>
<pre class="code ruby"><code class="ruby">puts <<-{HEREDOC}
cat #{__FILE__}
HEREDOC</code></pre>
<p>To call a method on a heredoc place it after the opening identifier:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_expected_result'>expected_result</span> <span class='op'>=</span> <span class='heredoc_beg'><<-EXPECTED</span>.<span class='id identifier rubyid_chomp'>chomp</span>
<span class='tstring_content'>One plus one is </span><span class='embexpr_beg'>#{</span><span class='int'>1</span> <span class='op'>+</span> <span class='int'>1</span><span class='embexpr_end'>}</span><span class='tstring_content'>
</span><span class='heredoc_end'>EXPECTED</span></code></pre>
<p>You may open multiple heredocs on the same line, but this can be difficult to read:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_puts'>puts</span>(<span class='heredoc_beg'><<-ONE</span><span class='comma'>,</span> <span class='heredoc_beg'><<-TWO</span>)
<span class='tstring_content'>content for heredoc one
</span><span class='heredoc_end'>ONE
</span><span class='tstring_content'>content for heredoc two
</span><span class='heredoc_end'>TWO</span></code></pre>
<h2 id="label-Symbols">Symbols</h2>
<p>A Symbol represents a name inside the ruby interpreter. See Symbol for more details on what symbols are and when ruby creates them internally.</p>
<p>You may reference a symbol using a colon: <code>:my_symbol</code>.</p>
<p>You may also create symbols by interpolation:</p>
<pre class="code ruby"><code class="ruby"><span class='symbeg'>:"</span><span class='tstring_content'>my_symbol1</span><span class='tstring_end'>"</span></span>
<span class='symbeg'>:"</span><span class='tstring_content'>my_symbol</span><span class='embexpr_beg'>#{</span><span class='int'>1</span> <span class='op'>+</span> <span class='int'>1</span><span class='embexpr_end'>}</span><span class='tstring_end'>"</span></span></code></pre>
<p>Like strings, a single-quote may be used to disable interpolation:</p>
<pre class="code ruby"><code class="ruby"><span class='symbeg'>:'</span><span class='tstring_content'>my_symbol#{1 + 1}</span><span class='tstring_end'>'</span></span> <span class='comment'>#=> :"my_symbol\#{1 + 1}"</span></code></pre>
<p>When creating a Hash, there is a special syntax for referencing a Symbol as well.</p>
<h2 id="label-Arrays">Arrays</h2>
<p>An array is created using the objects between <code>[</code> and <code>]</code>:</p>
<pre class="code ruby"><code class="ruby">[<span class='int'>1</span><span class='comma'>,</span> <span class='int'>2</span><span class='comma'>,</span> <span class='int'>3</span>]</code></pre>
<p>You may place expressions inside the array:</p>
<pre class="code ruby"><code class="ruby">[<span class='int'>1</span><span class='comma'>,</span> <span class='int'>1</span> <span class='op'>+</span> <span class='int'>1</span><span class='comma'>,</span> <span class='int'>1</span> <span class='op'>+</span> <span class='int'>2</span>]
[<span class='int'>1</span><span class='comma'>,</span> [<span class='int'>1</span> <span class='op'>+</span> <span class='int'>1</span><span class='comma'>,</span> [<span class='int'>1</span> <span class='op'>+</span> <span class='int'>2</span>]]]</code></pre>
<p>See Array for the methods you may use with an array.</p>
<h2 id="label-Hashes">Hashes</h2>
<p>A hash is created using key-value pairs between <code>{</code> and <code>}</code>:</p>
<pre class="code ruby"><code class="ruby">{ <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>a</span><span class='tstring_end'>"</span></span> <span class='op'>=></span> <span class='int'>1</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>b</span><span class='tstring_end'>"</span></span> <span class='op'>=></span> <span class='int'>2</span> }</code></pre>
<p>Both the key and value may be any object.</p>
<p>You can create a hash using symbol keys with the following syntax:</p>
<pre class="code ruby"><code class="ruby">{ <span class='label'>a:</span> <span class='int'>1</span><span class='comma'>,</span> <span class='label'>b:</span> <span class='int'>2</span> }</code></pre>
<p>This same syntax is used for keyword arguments for a method.</p>
<p>Like Symbol literals, you can quote symbol keys.</p>
<pre class="code ruby"><code class="ruby">{ <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>a 1</span><span class='label_end'>":</span> <span class='int'>1</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>b </span><span class='embexpr_beg'>#{</span><span class='int'>1</span> <span class='op'>+</span> <span class='int'>1</span><span class='embexpr_end'>}</span><span class='label_end'>":</span> <span class='int'>2</span> }</code></pre>
<p>is equal to</p>
<pre class="code ruby"><code class="ruby">{ <span class='symbeg'>:"</span><span class='tstring_content'>a 1</span><span class='tstring_end'>"</span></span> <span class='op'>=></span> <span class='int'>1</span><span class='comma'>,</span> <span class='symbeg'>:"</span><span class='tstring_content'>b 2</span><span class='tstring_end'>"</span></span> <span class='op'>=></span> <span class='int'>2</span> }</code></pre>
<p>See Hash for the methods you may use with a hash.</p>
<h2 id="label-Ranges">Ranges</h2>
<p>A range represents an interval of values. The range may include or exclude its ending value.</p>
<pre class="code ruby"><code class="ruby">(<span class='int'>1</span><span class='op'>..</span><span class='int'>2</span>) <span class='comment'># includes its ending value
</span>(<span class='int'>1</span><span class='op'>...</span><span class='int'>2</span>) <span class='comment'># excludes its ending value
</span>(<span class='int'>1</span><span class='op'>..</span>) <span class='comment'># endless range, representing infinite sequence from 1 to Infinity</span></code></pre>
<p>You may create a range of any object. See the Range documentation for details on the methods you need to implement.</p>
<h2 id="label-Regular+Expressions">Regular Expressions</h2>
<p>A regular expression is created using “/”:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>my regular expression</span><span class='regexp_end'>/</span></span></code></pre>
<p>The regular expression may be followed by flags which adjust the matching behavior of the regular expression. The “i” flag makes the regular expression case-insensitive:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>my regular expression</span><span class='regexp_end'>/i</span></span></code></pre>
<p>Interpolation may be used inside regular expressions along with escaped characters. Note that a regular expression may require additional escaped characters than a string.</p>
<p>See Regexp for a description of the syntax of regular expressions.</p>
<h2 id="label-Procs">Procs</h2>
<p>A proc can be created with <code>-></code>:</p>
<pre class="code ruby"><code class="ruby"><span class='comment'>#=> { 1 + 1 }</span></code></pre>
<p>Calling the above proc will give a result of <code>2</code>.</p>
<p>You can require arguments for the proc as follows:</p>
<pre class="code ruby"><code class="ruby"><span class='tlambda'>-></span>(<span class='id identifier rubyid_v'>v</span>) <span class='tlambeg'>{</span> <span class='int'>1</span> <span class='op'>+</span> <span class='id identifier rubyid_v'>v</span> }</code></pre>
<p>This proc will add one to its argument.</p>
<h2 id="label-Percent+Strings">Percent Strings</h2>
<p>Besides <code>%(...)</code> which creates a String, the <code>%</code> may create other types of object. As with strings, an uppercase letter allows interpolation and escaped characters while a lowercase letter disables them.</p>
<p>These are the types of percent strings in ruby:</p>
<dl class="rdoc-list note-list"><dt><code>%i</code>
<dd>
<p>Array of Symbols</p>
</dd><dt><code>%q</code>
<dd>
<p>String</p>
</dd><dt><code>%r</code>
<dd>
<p>Regular Expression</p>
</dd><dt><code>%s</code>
<dd>
<p>Symbol</p>
</dd><dt><code>%w</code>
<dd>
<p>Array of Strings</p>
</dd><dt><code>%x</code>
<dd>
<p>Backtick (capture subshell result)</p>
</dd></dl>
<p>For the two array forms of percent string, if you wish to include a space in one of the array entries you must escape it with a “\” character:</p>
<pre class="code ruby"><code class="ruby"><span class='qwords'><span class='qwords_beg'>%w[</span><span class='tstring_content'>one</span><span class='words_sep'> </span><span class='tstring_content'>one-hundred\ one</span><span class='tstring_end'>]</span></span>
<span class='comment'>#=> ["one", "one-hundred one"]</span></code></pre>
<p>If you are using “(”, “[”, “<code>“<” you must close it with “)”, “]”, “</code>”, “>” respectively. You may use most other non-alphanumeric characters for percent string delimiters such as “%”, “|”, “^”, etc.</p>
<div id='footer'></div>
</div> <!-- content -->
</div> <!-- y_main -->
</body>
</html>