-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfile.modules_and_classes.html
359 lines (249 loc) · 20.3 KB
/
file.modules_and_classes.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
<!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: Modules and Classes — 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 = "modules_and_classes",
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: Modules and Classes ▲</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-Modules">Modules</h1>
<p>Modules serve two purposes in Ruby, namespacing and mix-in functionality.</p>
<p>A namespace can be used to organize code by package or functionality that separates common names from interference by other packages. For example, the IRB namespace provides functionality for irb that prevents a collision for the common name “Context”.</p>
<p>Mix-in functionality allows sharing common methods across multiple classes or modules. Ruby comes with the Enumerable mix-in module which provides many enumeration methods based on the <code>each</code> method and Comparable allows comparison of objects based on the <code><=></code> comparison method.</p>
<p>Note that there are many similarities between modules and classes. Besides the ability to mix-in a module, the description of modules below also applies to classes.</p>
<h2 id="label-Module+Definition">Module Definition</h2>
<p>A module is created using the <code>module</code> keyword:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>module</span> <span class='const'>MyModule</span>
<span class='comment'># ...
</span><span class='kw'>end</span></code></pre>
<p>A module may be reopened any number of times to add, change or remove functionality:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>module</span> <span class='const'>MyModule</span>
<span class='kw'>def</span> <span class='id identifier rubyid_my_method'>my_method</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='kw'>module</span> <span class='const'>MyModule</span>
<span class='kw'>alias</span> <span class='id identifier rubyid_my_alias'>my_alias</span> <span class='id identifier rubyid_my_method'>my_method</span>
<span class='kw'>end</span>
<span class='kw'>module</span> <span class='const'>MyModule</span>
<span class='id identifier rubyid_remove_method'>remove_method</span> <span class='symbeg'>:</span><span class='id identifier rubyid_my_method'>my_method</span>
<span class='kw'>end</span></code></pre>
<p>Reopening classes is a very powerful feature of Ruby, but it is best to only reopen classes you own. Reopening classes you do not own may lead to naming conflicts or difficult to diagnose bugs.</p>
<h2 id="label-Nesting">Nesting</h2>
<p>Modules may be nested:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>module</span> <span class='const'>Outer</span>
<span class='kw'>module</span> <span class='const'>Inner</span>
<span class='kw'>end</span>
<span class='kw'>end</span></code></pre>
<p>Many packages create a single outermost module (or class) to provide a namespace for their functionality.</p>
<p>You may also define inner modules using <code>::</code> provided the outer modules (or classes) are already defined:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>module</span> <span class='const'>Outer</span><span class='op'>::</span><span class='const'>Inner</span><span class='op'>::</span><span class='const'>GrandChild</span>
<span class='kw'>end</span></code></pre>
<p>Note that this will raise a <code>NameError</code> if <code>Outer</code> and <code>Outer::Inner</code> are not already defined.</p>
<p>This style has the benefit of allowing the author to reduce the amount of indentation. Instead of 3 levels of indentation only one is necessary. However, the scope of constant lookup is different for creating a namespace using this syntax instead of the more verbose syntax.</p>
<h2 id="label-Scope">Scope</h2>
<h3 id="label-self"><code>self</code></h3>
<p><code>self</code> refers to the object that defines the current scope. <code>self</code> will change when entering a different method or when defining a new module.</p>
<h3 id="label-Constants">Constants</h3>
<p>Accessible constants are different depending on the module nesting (which syntax was used to define the module). In the following example the constant <code>A::Z</code> is accessible from B as A is part of the nesting:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>module</span> <span class='const'>A</span>
<span class='const'>Z</span> <span class='op'>=</span> <span class='int'>1</span>
<span class='kw'>module</span> <span class='const'>B</span>
<span class='id identifier rubyid_p'>p</span> <span class='const'>Module</span>.<span class='id identifier rubyid_nesting'>nesting</span> <span class='comment'>#=> [A::B, A]
</span> <span class='id identifier rubyid_p'>p</span> <span class='const'>Z</span> <span class='comment'>#=> 1
</span> <span class='kw'>end</span>
<span class='kw'>end</span></code></pre>
<p>However, if you use <code>::</code> to define <code>A::B</code> without nesting it inside <code>A</code>, a NameError exception will be raised because the nesting does not include <code>A</code>:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>module</span> <span class='const'>A</span>
<span class='const'>Z</span> <span class='op'>=</span> <span class='int'>1</span>
<span class='kw'>end</span>
<span class='kw'>module</span> <span class='const'>A</span><span class='op'>::</span><span class='const'>B</span>
<span class='id identifier rubyid_p'>p</span> <span class='const'>Module</span>.<span class='id identifier rubyid_nesting'>nesting</span> <span class='comment'>#=> [A::B]
</span> <span class='id identifier rubyid_p'>p</span> <span class='const'>Z</span> <span class='comment'>#=> raises NameError
</span><span class='kw'>end</span></code></pre>
<p>If a constant is defined at the top-level you may preceded it with <code>::</code> to reference it:</p>
<pre class="code ruby"><code class="ruby"><span class='const'>Z</span> <span class='op'>=</span> <span class='int'>0</span>
<span class='kw'>module</span> <span class='const'>A</span>
<span class='const'>Z</span> <span class='op'>=</span> <span class='int'>1</span>
<span class='kw'>module</span> <span class='const'>B</span>
<span class='id identifier rubyid_p'>p</span> <span class='op'>::</span><span class='const'>Z</span> <span class='comment'>#=> 0
</span> <span class='kw'>end</span>
<span class='kw'>end</span></code></pre>
<h3 id="label-Methods">Methods</h3>
<p>For method definition documentation see the <a href="file.methods.html" title="syntax documentation for methods">syntax documentation for methods</a>.</p>
<p>Class methods may be called directly. (This is slightly confusing, but a method on a module is often called a “class method” instead of a “module method”. See also Module#module_function which can convert an instance method into a class method.)</p>
<p>When a class method references a constant, it uses the same rules as referencing it outside the method as the scope is the same.</p>
<p>Instance methods defined in a module are only callable when included. These methods have access to the constants defined when they were included through the ancestors list:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>module</span> <span class='const'>A</span>
<span class='const'>Z</span> <span class='op'>=</span> <span class='int'>1</span>
<span class='kw'>def</span> <span class='id identifier rubyid_z'>z</span>
<span class='const'>Z</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_include'>include</span> <span class='const'>A</span>
<span class='id identifier rubyid_p'>p</span> <span class='kw'>self</span>.<span class='id identifier rubyid_class'>class</span>.<span class='id identifier rubyid_ancestors'>ancestors</span> <span class='comment'>#=> [Object, A, Kernel, BasicObject]
</span><span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_z'>z</span> <span class='comment'>#=> 1</span></code></pre>
<h3 id="label-Visibility">Visibility</h3>
<p>Ruby has three types of visibility. The default is <code>public</code>. A public method may be called from any other object.</p>
<p>The second visibility is <code>protected</code>. When calling a protected method the sender must be a subclass of the receiver or the receiver must be a subclass of the sender. Otherwise a NoMethodError will be raised.</p>
<p>Protected visibility is most frequently used to define <code>==</code> and other comparison methods where the author does not wish to expose an object’s state to any caller and would like to restrict it only to inherited classes.</p>
<p>Here is an example:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>A</span>
<span class='kw'>def</span> <span class='id identifier rubyid_n'>n</span>(<span class='id identifier rubyid_other'>other</span>)
<span class='id identifier rubyid_other'>other</span>.<span class='id identifier rubyid_m'>m</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='kw'>class</span> <span class='const'>B</span> <span class='op'><</span> <span class='const'>A</span>
<span class='kw'>def</span> <span class='id identifier rubyid_m'>m</span>
<span class='int'>1</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_protected'>protected</span> <span class='symbeg'>:</span><span class='id identifier rubyid_m'>m</span>
<span class='kw'>end</span>
<span class='kw'>class</span> <span class='const'>C</span> <span class='op'><</span> <span class='const'>B</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_a'>a</span> <span class='op'>=</span> <span class='const'>A</span>.<span class='id identifier rubyid_new'>new</span>
<span class='id identifier rubyid_b'>b</span> <span class='op'>=</span> <span class='const'>B</span>.<span class='id identifier rubyid_new'>new</span>
<span class='id identifier rubyid_c'>c</span> <span class='op'>=</span> <span class='const'>C</span>.<span class='id identifier rubyid_new'>new</span>
<span class='id identifier rubyid_c'>c</span>.<span class='id identifier rubyid_n'>n</span> <span class='id identifier rubyid_b'>b</span> <span class='comment'>#=> 1 -- C is a subclass of B
</span><span class='id identifier rubyid_b'>b</span>.<span class='id identifier rubyid_n'>n</span> <span class='id identifier rubyid_b'>b</span> <span class='comment'>#=> 1 -- m called on defining class
</span><span class='id identifier rubyid_a'>a</span>.<span class='id identifier rubyid_n'>n</span> <span class='id identifier rubyid_b'>b</span> <span class='comment'># raises NoMethodError A is not a subclass of B</span></code></pre>
<p>The third visibility is <code>private</code>. A private method may not be called with a receiver, not even <code>self</code>. If a private method is called with a receiver a NoMethodError will be raised.</p>
<h3 id="label-alias+and+undef"><code>alias</code> and <code>undef</code></h3>
<p>You may also alias or undefine methods, but these operations are not restricted to modules or classes. See the <a href="file.miscellaneous.html" title="miscellaneous syntax section">miscellaneous syntax section</a> for documentation.</p>
<h1 id="label-Classes">Classes</h1>
<p>Every class is also a module, but unlike modules a class may not be mixed-in to another module (or class). Like a module, a class can be used as a namespace. A class also inherits methods and constants from its superclass.</p>
<h2 id="label-Defining+a+class">Defining a class</h2>
<p>Use the <code>class</code> keyword to create a class:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>MyClass</span>
<span class='comment'># ...
</span><span class='kw'>end</span></code></pre>
<p>If you do not supply a superclass your new class will inherit from Object. You may inherit from a different class using <code><</code> followed by a class name:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>MySubclass</span> <span class='op'><</span> <span class='const'>MyClass</span>
<span class='comment'># ...
</span><span class='kw'>end</span></code></pre>
<p>There is a special class BasicObject which is designed as a blank class and includes a minimum of built-in methods. You can use BasicObject to create an independent inheritance structure. See the BasicObject documentation for further details.</p>
<h2 id="label-Inheritance">Inheritance</h2>
<p>Any method defined on a class is callable from its subclass:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>A</span>
<span class='const'>Z</span> <span class='op'>=</span> <span class='int'>1</span>
<span class='kw'>def</span> <span class='id identifier rubyid_z'>z</span>
<span class='const'>Z</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='kw'>class</span> <span class='const'>B</span> <span class='op'><</span> <span class='const'>A</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_p'>p</span> <span class='const'>B</span>.<span class='id identifier rubyid_new'>new</span>.<span class='id identifier rubyid_z'>z</span> <span class='comment'>#=> 1</span></code></pre>
<p>The same is true for constants:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>A</span>
<span class='const'>Z</span> <span class='op'>=</span> <span class='int'>1</span>
<span class='kw'>end</span>
<span class='kw'>class</span> <span class='const'>B</span> <span class='op'><</span> <span class='const'>A</span>
<span class='kw'>def</span> <span class='id identifier rubyid_z'>z</span>
<span class='const'>Z</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_p'>p</span> <span class='const'>B</span>.<span class='id identifier rubyid_new'>new</span>.<span class='id identifier rubyid_z'>z</span> <span class='comment'>#=> 1</span></code></pre>
<p>You can override the functionality of a superclass method by redefining the method:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>A</span>
<span class='kw'>def</span> <span class='id identifier rubyid_m'>m</span>
<span class='int'>1</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='kw'>class</span> <span class='const'>B</span> <span class='op'><</span> <span class='const'>A</span>
<span class='kw'>def</span> <span class='id identifier rubyid_m'>m</span>
<span class='int'>2</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_p'>p</span> <span class='const'>B</span>.<span class='id identifier rubyid_new'>new</span>.<span class='id identifier rubyid_m'>m</span> <span class='comment'>#=> 2</span></code></pre>
<p>If you wish to invoke the superclass functionality from a method use <code>super</code>:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>A</span>
<span class='kw'>def</span> <span class='id identifier rubyid_m'>m</span>
<span class='int'>1</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='kw'>class</span> <span class='const'>B</span> <span class='op'><</span> <span class='const'>A</span>
<span class='kw'>def</span> <span class='id identifier rubyid_m'>m</span>
<span class='int'>2</span> <span class='op'>+</span> <span class='kw'>super</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_p'>p</span> <span class='const'>B</span>.<span class='id identifier rubyid_new'>new</span>.<span class='id identifier rubyid_m'>m</span> <span class='comment'>#=> 3</span></code></pre>
<p>When used without any arguments <code>super</code> uses the arguments given to the subclass method. To send no arguments to the superclass method use <code>super()</code>. To send specific arguments to the superclass method provide them manually like <code>super(2)</code>.</p>
<p><code>super</code> may be called as many times as you like in the subclass method.</p>
<h1 id="label-Singleton+Classes">Singleton Classes</h1>
<p>The singleton class (also known as the metaclass or eigenclass) of an object is a class that holds methods for only that instance. You can access the singleton class of an object using <code>class << object</code> like this:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>C</span>
<span class='kw'>end</span>
<span class='kw'>class</span> <span class='op'><<</span> <span class='const'>C</span>
<span class='comment'># self is the singleton class here
</span><span class='kw'>end</span></code></pre>
<p>Most frequently you’ll see the singleton class accessed like this:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>C</span>
<span class='kw'>class</span> <span class='op'><<</span> <span class='kw'>self</span>
<span class='comment'># ...
</span> <span class='kw'>end</span>
<span class='kw'>end</span></code></pre>
<p>This allows definition of methods and attributes on a class (or module) without needing to write <code>def self.my_method</code>.</p>
<p>Since you can open the singleton class of any object this means that this code block:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_o'>o</span> <span class='op'>=</span> <span class='const'>Object</span>.<span class='id identifier rubyid_new'>new</span>
<span class='kw'>def</span> <span class='id identifier rubyid_o'>o</span>.<span class='id identifier rubyid_my_method'>my_method</span>
<span class='int'>1</span> <span class='op'>+</span> <span class='int'>1</span>
<span class='kw'>end</span></code></pre>
<p>is equivalent to this code block:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_o'>o</span> <span class='op'>=</span> <span class='const'>Object</span>.<span class='id identifier rubyid_new'>new</span>
<span class='kw'>class</span> <span class='op'><<</span> <span class='id identifier rubyid_o'>o</span>
<span class='kw'>def</span> <span class='id identifier rubyid_my_method'>my_method</span>
<span class='int'>1</span> <span class='op'>+</span> <span class='int'>1</span>
<span class='kw'>end</span>
<span class='kw'>end</span></code></pre>
<p>Both objects will have a <code>my_method</code> that returns <code>2</code>.</p>
<div id='footer'></div>
</div> <!-- content -->
</div> <!-- y_main -->
</body>
</html>