rubycube 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGES +30 -0
- data/MANIFEST +12 -0
- data/README.md +69 -0
- data/Rakefile +50 -0
- data/certs/djberg96_pub.pem +21 -0
- data/cube.gemspec +23 -0
- data/doc/Bar.html +102 -0
- data/doc/CHANGES.html +159 -0
- data/doc/Foo.html +184 -0
- data/doc/Interface.html +278 -0
- data/doc/Interface/MethodMissing.html +104 -0
- data/doc/Interface/PrivateVisibleMethodMissing.html +102 -0
- data/doc/Interface/PublicVisibleMethodMissing.html +102 -0
- data/doc/MANIFEST.html +114 -0
- data/doc/Module.html +153 -0
- data/doc/MyClass.html +234 -0
- data/doc/MyInterface.html +106 -0
- data/doc/MySubInterface.html +110 -0
- data/doc/Object.html +299 -0
- data/doc/README.html +222 -0
- data/doc/Rakefile.html +148 -0
- data/doc/TC_Interface.html +438 -0
- data/doc/certs/djberg96_pub_pem.html +110 -0
- data/doc/created.rid +13 -0
- data/doc/css/fonts.css +167 -0
- data/doc/css/rdoc.css +590 -0
- data/doc/fonts/Lato-Light.ttf +0 -0
- data/doc/fonts/Lato-LightItalic.ttf +0 -0
- data/doc/fonts/Lato-Regular.ttf +0 -0
- data/doc/fonts/Lato-RegularItalic.ttf +0 -0
- data/doc/fonts/SourceCodePro-Bold.ttf +0 -0
- data/doc/fonts/SourceCodePro-Regular.ttf +0 -0
- data/doc/images/add.png +0 -0
- data/doc/images/arrow_up.png +0 -0
- data/doc/images/brick.png +0 -0
- data/doc/images/brick_link.png +0 -0
- data/doc/images/bug.png +0 -0
- data/doc/images/bullet_black.png +0 -0
- data/doc/images/bullet_toggle_minus.png +0 -0
- data/doc/images/bullet_toggle_plus.png +0 -0
- data/doc/images/date.png +0 -0
- data/doc/images/delete.png +0 -0
- data/doc/images/find.png +0 -0
- data/doc/images/loadingAnimation.gif +0 -0
- data/doc/images/macFFBgHack.png +0 -0
- data/doc/images/package.png +0 -0
- data/doc/images/page_green.png +0 -0
- data/doc/images/page_white_text.png +0 -0
- data/doc/images/page_white_width.png +0 -0
- data/doc/images/plugin.png +0 -0
- data/doc/images/ruby.png +0 -0
- data/doc/images/tag_blue.png +0 -0
- data/doc/images/tag_green.png +0 -0
- data/doc/images/transparent.png +0 -0
- data/doc/images/wrench.png +0 -0
- data/doc/images/wrench_orange.png +0 -0
- data/doc/images/zoom.png +0 -0
- data/doc/index.html +121 -0
- data/doc/interface_gemspec.html +121 -0
- data/doc/js/darkfish.js +161 -0
- data/doc/js/jquery.js +4 -0
- data/doc/js/navigation.js +142 -0
- data/doc/js/navigation.js.gz +0 -0
- data/doc/js/search.js +109 -0
- data/doc/js/search_index.js +1 -0
- data/doc/js/search_index.js.gz +0 -0
- data/doc/js/searcher.js +228 -0
- data/doc/js/searcher.js.gz +0 -0
- data/doc/table_of_contents.html +227 -0
- data/examples/demo.rb +96 -0
- data/lib/cube.rb +2 -0
- data/lib/cube/interfaces.rb +266 -0
- data/lib/cube/traits.rb +75 -0
- data/test/test_interface.rb +111 -0
- metadata +151 -0
data/doc/Object.html
ADDED
@@ -0,0 +1,299 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
|
3
|
+
<html>
|
4
|
+
<head>
|
5
|
+
<meta charset="UTF-8">
|
6
|
+
|
7
|
+
<title>class Object - RDoc Documentation</title>
|
8
|
+
|
9
|
+
<script type="text/javascript">
|
10
|
+
var rdoc_rel_prefix = "./";
|
11
|
+
</script>
|
12
|
+
|
13
|
+
<script src="./js/jquery.js"></script>
|
14
|
+
<script src="./js/darkfish.js"></script>
|
15
|
+
|
16
|
+
<link href="./css/fonts.css" rel="stylesheet">
|
17
|
+
<link href="./css/rdoc.css" rel="stylesheet">
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
<body id="top" role="document" class="class">
|
22
|
+
<nav role="navigation">
|
23
|
+
<div id="project-navigation">
|
24
|
+
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
|
25
|
+
<h2>
|
26
|
+
<a href="./index.html" rel="home">Home</a>
|
27
|
+
</h2>
|
28
|
+
|
29
|
+
<div id="table-of-contents-navigation">
|
30
|
+
<a href="./table_of_contents.html#pages">Pages</a>
|
31
|
+
<a href="./table_of_contents.html#classes">Classes</a>
|
32
|
+
<a href="./table_of_contents.html#methods">Methods</a>
|
33
|
+
</div>
|
34
|
+
</div>
|
35
|
+
|
36
|
+
<div id="search-section" role="search" class="project-section initially-hidden">
|
37
|
+
<form action="#" method="get" accept-charset="utf-8">
|
38
|
+
<div id="search-field-wrapper">
|
39
|
+
<input id="search-field" role="combobox" aria-label="Search"
|
40
|
+
aria-autocomplete="list" aria-controls="search-results"
|
41
|
+
type="text" name="search" placeholder="Search" spellcheck="false"
|
42
|
+
title="Type to search, Up and Down to navigate, Enter to load">
|
43
|
+
</div>
|
44
|
+
|
45
|
+
<ul id="search-results" aria-label="Search Results"
|
46
|
+
aria-busy="false" aria-expanded="false"
|
47
|
+
aria-atomic="false" class="initially-hidden"></ul>
|
48
|
+
</form>
|
49
|
+
</div>
|
50
|
+
|
51
|
+
</div>
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
<div id="class-metadata">
|
56
|
+
|
57
|
+
<div id="parent-class-section" class="nav-section">
|
58
|
+
<h3>Parent</h3>
|
59
|
+
|
60
|
+
|
61
|
+
<p class="link">BasicObject
|
62
|
+
|
63
|
+
</div>
|
64
|
+
|
65
|
+
|
66
|
+
<div id="extends-section" class="nav-section">
|
67
|
+
<h3>Extended With Modules</h3>
|
68
|
+
|
69
|
+
<ul class="link-list">
|
70
|
+
|
71
|
+
|
72
|
+
<li><a class="extend" href="Interface.html">Interface</a>
|
73
|
+
|
74
|
+
|
75
|
+
</ul>
|
76
|
+
</div>
|
77
|
+
|
78
|
+
<!-- Method Quickref -->
|
79
|
+
<div id="method-list-section" class="nav-section">
|
80
|
+
<h3>Methods</h3>
|
81
|
+
|
82
|
+
<ul class="link-list" role="directory">
|
83
|
+
|
84
|
+
<li ><a href="#method-i-check_class">#check_class</a>
|
85
|
+
|
86
|
+
<li ><a href="#method-i-check_interface">#check_interface</a>
|
87
|
+
|
88
|
+
<li ><a href="#method-i-interface">#interface</a>
|
89
|
+
|
90
|
+
</ul>
|
91
|
+
</div>
|
92
|
+
|
93
|
+
</div>
|
94
|
+
</nav>
|
95
|
+
|
96
|
+
<main role="main" aria-labelledby="class-Object">
|
97
|
+
<h1 id="class-Object" class="class">
|
98
|
+
class Object
|
99
|
+
</h1>
|
100
|
+
|
101
|
+
<section class="description">
|
102
|
+
|
103
|
+
</section>
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
<section id="5Buntitled-5D" class="documentation-section">
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
|
114
|
+
<section class="constants-list">
|
115
|
+
<header>
|
116
|
+
<h3>Constants</h3>
|
117
|
+
</header>
|
118
|
+
<dl>
|
119
|
+
|
120
|
+
<dt id="MyInterface">MyInterface
|
121
|
+
|
122
|
+
<dd>
|
123
|
+
|
124
|
+
|
125
|
+
<dt id="MySubInterface">MySubInterface
|
126
|
+
|
127
|
+
<dd><p>require foo and baz, but not bar</p>
|
128
|
+
|
129
|
+
|
130
|
+
</dl>
|
131
|
+
</section>
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
<section id="public-instance-5Buntitled-5D-method-details" class="method-section">
|
138
|
+
<header>
|
139
|
+
<h3>Public Instance Methods</h3>
|
140
|
+
</header>
|
141
|
+
|
142
|
+
|
143
|
+
<div id="method-i-check_class" class="method-detail ">
|
144
|
+
|
145
|
+
<div class="method-heading">
|
146
|
+
<span class="method-name">check_class</span><span
|
147
|
+
class="method-args">() { || ... }</span>
|
148
|
+
|
149
|
+
<span class="method-click-advice">click to toggle source</span>
|
150
|
+
|
151
|
+
</div>
|
152
|
+
|
153
|
+
|
154
|
+
<div class="method-description">
|
155
|
+
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
<div class="method-source-code" id="check_class-source">
|
162
|
+
<pre><span class="ruby-comment"># File lib/interface.rb, line 142</span>
|
163
|
+
<span class="ruby-keyword">def</span> <span class="ruby-identifier">check_class</span>
|
164
|
+
<span class="ruby-identifier">spec</span> = <span class="ruby-keyword">yield</span>
|
165
|
+
<span class="ruby-identifier">spec</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">type</span>, <span class="ruby-identifier">k</span><span class="ruby-operator">|</span>
|
166
|
+
<span class="ruby-identifier">fail</span> <span class="ruby-constant">NameError</span>, <span class="ruby-node">"#{type} is not a class"</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">type</span>.<span class="ruby-identifier">is_a?</span> <span class="ruby-constant">Class</span>
|
167
|
+
<span class="ruby-identifier">fail</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-node">"#{k} is not type #{type}"</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">k</span>.<span class="ruby-identifier">is_a?</span> <span class="ruby-identifier">type</span>
|
168
|
+
<span class="ruby-keyword">end</span>
|
169
|
+
<span class="ruby-keyword">end</span></pre>
|
170
|
+
</div>
|
171
|
+
|
172
|
+
</div>
|
173
|
+
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
</div>
|
178
|
+
|
179
|
+
|
180
|
+
<div id="method-i-check_interface" class="method-detail ">
|
181
|
+
|
182
|
+
<div class="method-heading">
|
183
|
+
<span class="method-name">check_interface</span><span
|
184
|
+
class="method-args">() { || ... }</span>
|
185
|
+
|
186
|
+
<span class="method-click-advice">click to toggle source</span>
|
187
|
+
|
188
|
+
</div>
|
189
|
+
|
190
|
+
|
191
|
+
<div class="method-description">
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
|
198
|
+
<div class="method-source-code" id="check_interface-source">
|
199
|
+
<pre><span class="ruby-comment"># File lib/interface.rb, line 150</span>
|
200
|
+
<span class="ruby-keyword">def</span> <span class="ruby-identifier">check_interface</span>
|
201
|
+
<span class="ruby-identifier">spec</span> = <span class="ruby-keyword">yield</span>
|
202
|
+
<span class="ruby-identifier">spec</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">type</span>, <span class="ruby-identifier">k</span><span class="ruby-operator">|</span>
|
203
|
+
<span class="ruby-identifier">fail</span> <span class="ruby-constant">NameError</span>, <span class="ruby-node">"#{type} is not an interface"</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">type</span>.<span class="ruby-identifier">is_a?</span> <span class="ruby-constant">Module</span>
|
204
|
+
<span class="ruby-keyword">unless</span> <span class="ruby-identifier">k</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">include?</span> <span class="ruby-identifier">type</span>
|
205
|
+
<span class="ruby-identifier">fail</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-node">"#{k} does not implement #{type}"</span>
|
206
|
+
<span class="ruby-keyword">end</span>
|
207
|
+
<span class="ruby-keyword">end</span>
|
208
|
+
<span class="ruby-keyword">end</span></pre>
|
209
|
+
</div>
|
210
|
+
|
211
|
+
</div>
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
|
216
|
+
</div>
|
217
|
+
|
218
|
+
|
219
|
+
<div id="method-i-interface" class="method-detail ">
|
220
|
+
|
221
|
+
<div class="method-heading">
|
222
|
+
<span class="method-name">interface</span><span
|
223
|
+
class="method-args">(&block)</span>
|
224
|
+
|
225
|
+
<span class="method-click-advice">click to toggle source</span>
|
226
|
+
|
227
|
+
</div>
|
228
|
+
|
229
|
+
|
230
|
+
<div class="method-description">
|
231
|
+
|
232
|
+
<p>The interface method creates an interface module which typically sets a
|
233
|
+
list of methods that must be defined in the including class or module. If
|
234
|
+
the methods are not defined, an <a
|
235
|
+
href="Interface/MethodMissing.html">Interface::MethodMissing</a> error is
|
236
|
+
raised.</p>
|
237
|
+
|
238
|
+
<p>A interface can extend an existing interface as well. These are called
|
239
|
+
sub-interfaces, and they can included the rules for their parent interface
|
240
|
+
by simply extending it.</p>
|
241
|
+
|
242
|
+
<p>Example:</p>
|
243
|
+
|
244
|
+
<pre class="ruby"><span class="ruby-comment"># Require 'alpha' and 'beta' methods</span>
|
245
|
+
<span class="ruby-constant">AlphaInterface</span> = <span class="ruby-identifier">interface</span>{
|
246
|
+
<span class="ruby-identifier">public_visible</span> :<span class="ruby-identifier">alpha</span>, :<span class="ruby-identifier">beta</span>
|
247
|
+
}
|
248
|
+
|
249
|
+
<span class="ruby-comment"># A sub-interface that requires 'beta' and 'gamma' only</span>
|
250
|
+
<span class="ruby-constant">GammaInterface</span> = <span class="ruby-identifier">interface</span>{
|
251
|
+
<span class="ruby-identifier">extends</span> <span class="ruby-constant">AlphaInterface</span>
|
252
|
+
<span class="ruby-identifier">public_visible</span> :<span class="ruby-identifier">gamma</span>
|
253
|
+
<span class="ruby-identifier">unrequired_methods</span> :<span class="ruby-identifier">alpha</span>
|
254
|
+
}
|
255
|
+
|
256
|
+
<span class="ruby-comment"># Raises an Interface::MethodMissing error because :beta is not defined.</span>
|
257
|
+
<span class="ruby-keyword">class</span> <span class="ruby-constant">MyClass</span>
|
258
|
+
<span class="ruby-keyword">def</span> <span class="ruby-identifier">alpha</span>
|
259
|
+
<span class="ruby-comment"># ...</span>
|
260
|
+
<span class="ruby-keyword">end</span>
|
261
|
+
<span class="ruby-identifier">implements</span> <span class="ruby-constant">AlphaInterface</span>
|
262
|
+
<span class="ruby-keyword">end</span>
|
263
|
+
</pre>
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
|
268
|
+
<div class="method-source-code" id="interface-source">
|
269
|
+
<pre><span class="ruby-comment"># File lib/interface.rb, line 132</span>
|
270
|
+
<span class="ruby-keyword">def</span> <span class="ruby-identifier">interface</span>(<span class="ruby-operator">&</span><span class="ruby-identifier">block</span>)
|
271
|
+
<span class="ruby-identifier">mod</span> = <span class="ruby-constant">Module</span>.<span class="ruby-identifier">new</span>
|
272
|
+
<span class="ruby-identifier">mod</span>.<span class="ruby-identifier">extend</span>(<span class="ruby-constant">Interface</span>)
|
273
|
+
<span class="ruby-identifier">mod</span>.<span class="ruby-identifier">instance_variable_set</span>(<span class="ruby-string">'@ids'</span>, [])
|
274
|
+
<span class="ruby-identifier">mod</span>.<span class="ruby-identifier">instance_variable_set</span>(<span class="ruby-string">'@private_ids'</span>, [])
|
275
|
+
<span class="ruby-identifier">mod</span>.<span class="ruby-identifier">instance_eval</span>(<span class="ruby-operator">&</span><span class="ruby-identifier">block</span>)
|
276
|
+
<span class="ruby-identifier">mod</span>
|
277
|
+
<span class="ruby-keyword">end</span></pre>
|
278
|
+
</div>
|
279
|
+
|
280
|
+
</div>
|
281
|
+
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
</div>
|
286
|
+
|
287
|
+
|
288
|
+
</section>
|
289
|
+
|
290
|
+
</section>
|
291
|
+
</main>
|
292
|
+
|
293
|
+
|
294
|
+
<footer id="validator-badges" role="contentinfo">
|
295
|
+
<p><a href="http://validator.w3.org/check/referer">Validate</a>
|
296
|
+
<p>Generated by <a href="http://docs.seattlerb.org/rdoc/">RDoc</a> 4.2.0.
|
297
|
+
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
|
298
|
+
</footer>
|
299
|
+
|
data/doc/README.html
ADDED
@@ -0,0 +1,222 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
|
3
|
+
<html>
|
4
|
+
<head>
|
5
|
+
<meta charset="UTF-8">
|
6
|
+
|
7
|
+
<title>README - RDoc Documentation</title>
|
8
|
+
|
9
|
+
<script type="text/javascript">
|
10
|
+
var rdoc_rel_prefix = "./";
|
11
|
+
</script>
|
12
|
+
|
13
|
+
<script src="./js/jquery.js"></script>
|
14
|
+
<script src="./js/darkfish.js"></script>
|
15
|
+
|
16
|
+
<link href="./css/fonts.css" rel="stylesheet">
|
17
|
+
<link href="./css/rdoc.css" rel="stylesheet">
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
<body id="top" role="document" class="file">
|
22
|
+
<nav role="navigation">
|
23
|
+
<div id="project-navigation">
|
24
|
+
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
|
25
|
+
<h2>
|
26
|
+
<a href="./index.html" rel="home">Home</a>
|
27
|
+
</h2>
|
28
|
+
|
29
|
+
<div id="table-of-contents-navigation">
|
30
|
+
<a href="./table_of_contents.html#pages">Pages</a>
|
31
|
+
<a href="./table_of_contents.html#classes">Classes</a>
|
32
|
+
<a href="./table_of_contents.html#methods">Methods</a>
|
33
|
+
</div>
|
34
|
+
</div>
|
35
|
+
|
36
|
+
<div id="search-section" role="search" class="project-section initially-hidden">
|
37
|
+
<form action="#" method="get" accept-charset="utf-8">
|
38
|
+
<div id="search-field-wrapper">
|
39
|
+
<input id="search-field" role="combobox" aria-label="Search"
|
40
|
+
aria-autocomplete="list" aria-controls="search-results"
|
41
|
+
type="text" name="search" placeholder="Search" spellcheck="false"
|
42
|
+
title="Type to search, Up and Down to navigate, Enter to load">
|
43
|
+
</div>
|
44
|
+
|
45
|
+
<ul id="search-results" aria-label="Search Results"
|
46
|
+
aria-busy="false" aria-expanded="false"
|
47
|
+
aria-atomic="false" class="initially-hidden"></ul>
|
48
|
+
</form>
|
49
|
+
</div>
|
50
|
+
|
51
|
+
</div>
|
52
|
+
|
53
|
+
|
54
|
+
<div class="nav-section">
|
55
|
+
<h3>Table of Contents</h3>
|
56
|
+
|
57
|
+
<ul class="link-list" role="directory">
|
58
|
+
<li><a href="#label-Description">Description</a>
|
59
|
+
<li><a href="#label-Installation">Installation</a>
|
60
|
+
<li><a href="#label-Synopsis">Synopsis</a>
|
61
|
+
<li><a href="#label-General+Notes">General Notes</a>
|
62
|
+
<li><a href="#label-Runtime+performance+of+check+methods">Runtime performance of check methods</a>
|
63
|
+
<li><a href="#label-Developer-27s+Notes">Developer's Notes</a>
|
64
|
+
<li><a href="#label-Acknowledgements">Acknowledgements</a>
|
65
|
+
<li><a href="#label-Copyright">Copyright</a>
|
66
|
+
<li><a href="#label-Warranty">Warranty</a>
|
67
|
+
<li><a href="#label-License">License</a>
|
68
|
+
<li><a href="#label-Author">Author</a>
|
69
|
+
</ul>
|
70
|
+
</div>
|
71
|
+
|
72
|
+
|
73
|
+
<div id="project-metadata">
|
74
|
+
<div id="fileindex-section" class="nav-section">
|
75
|
+
<h3>Pages</h3>
|
76
|
+
|
77
|
+
<ul class="link-list">
|
78
|
+
|
79
|
+
<li><a href="./CHANGES.html">CHANGES</a>
|
80
|
+
|
81
|
+
<li><a href="./MANIFEST.html">MANIFEST</a>
|
82
|
+
|
83
|
+
<li><a href="./README.html">README</a>
|
84
|
+
|
85
|
+
<li><a href="./Rakefile.html">Rakefile</a>
|
86
|
+
|
87
|
+
<li><a href="./certs/djberg96_pub_pem.html">djberg96_pub.pem</a>
|
88
|
+
|
89
|
+
<li><a href="./interface_gemspec.html">interface.gemspec</a>
|
90
|
+
|
91
|
+
</ul>
|
92
|
+
</div>
|
93
|
+
|
94
|
+
</div>
|
95
|
+
</nav>
|
96
|
+
|
97
|
+
<main role="main" aria-label="Page README">
|
98
|
+
|
99
|
+
<h2 id="label-Description">Description<span><a href="#label-Description">¶</a> <a href="#top">↑</a></span></h2>
|
100
|
+
|
101
|
+
<pre>This module provides Java style interfaces for Ruby, including a fairly
|
102
|
+
similar syntax. I don't necessarily believe in interfaces, but I wanted to
|
103
|
+
put it out there as proof that it could be done. Frankly, Java needs mixins
|
104
|
+
more than Ruby needs interfaces, but here you go.</pre>
|
105
|
+
|
106
|
+
<h2 id="label-Installation">Installation<span><a href="#label-Installation">¶</a> <a href="#top">↑</a></span></h2>
|
107
|
+
|
108
|
+
<pre class="ruby"><span class="ruby-identifier">gem</span> <span class="ruby-identifier">install</span> <span class="ruby-identifier">interface</span>
|
109
|
+
</pre>
|
110
|
+
|
111
|
+
<h2 id="label-Synopsis">Synopsis<span><a href="#label-Synopsis">¶</a> <a href="#top">↑</a></span></h2>
|
112
|
+
|
113
|
+
<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'interface'</span>
|
114
|
+
|
115
|
+
<span class="ruby-constant">Calculator</span> = <span class="ruby-identifier">interface</span> {
|
116
|
+
<span class="ruby-identifier">public_visible</span> :<span class="ruby-identifier">fact</span>
|
117
|
+
}
|
118
|
+
|
119
|
+
<span class="ruby-keyword">class</span> <span class="ruby-constant">Calc</span>
|
120
|
+
<span class="ruby-keyword">def</span> <span class="ruby-identifier">fact</span>(<span class="ruby-identifier">n</span>)
|
121
|
+
<span class="ruby-identifier">res</span> = <span class="ruby-value">1</span>
|
122
|
+
<span class="ruby-identifier">n</span>.<span class="ruby-identifier">times</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span>
|
123
|
+
<span class="ruby-identifier">res</span> <span class="ruby-operator">*=</span> <span class="ruby-identifier">i</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
|
124
|
+
<span class="ruby-keyword">end</span>
|
125
|
+
<span class="ruby-identifier">res</span>
|
126
|
+
<span class="ruby-keyword">end</span>
|
127
|
+
|
128
|
+
<span class="ruby-comment"># This will check whether class Calc implements</span>
|
129
|
+
<span class="ruby-comment"># all methods mentioned in Calculator interface</span>
|
130
|
+
<span class="ruby-identifier">implements</span> <span class="ruby-constant">Calculator</span>
|
131
|
+
<span class="ruby-keyword">end</span>
|
132
|
+
|
133
|
+
<span class="ruby-keyword">def</span> <span class="ruby-identifier">do_fact</span>(<span class="ruby-identifier">calc</span>, <span class="ruby-identifier">n</span>)
|
134
|
+
<span class="ruby-comment"># check_interface and check_class do nothing unless</span>
|
135
|
+
<span class="ruby-comment"># environment variable RUBY_INTERFACE_TYPECHECK=1</span>
|
136
|
+
|
137
|
+
<span class="ruby-comment"># Check whether calc satisfies interface Calculator</span>
|
138
|
+
<span class="ruby-identifier">check_interface</span> { { <span class="ruby-constant">Calculator</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">calc</span> } }
|
139
|
+
<span class="ruby-comment"># Check whether n is of class Integer</span>
|
140
|
+
<span class="ruby-identifier">check_class</span> { { <span class="ruby-constant">Integer</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">n</span> } }
|
141
|
+
<span class="ruby-identifier">calc</span>.<span class="ruby-identifier">fact</span>(<span class="ruby-identifier">n</span>)
|
142
|
+
<span class="ruby-keyword">end</span>
|
143
|
+
|
144
|
+
<span class="ruby-identifier">do_sum</span>(<span class="ruby-constant">Calc</span>.<span class="ruby-identifier">new</span>, (<span class="ruby-value">1</span><span class="ruby-operator">...</span><span class="ruby-value">1000</span>).<span class="ruby-identifier">to_a</span>)
|
145
|
+
</pre>
|
146
|
+
|
147
|
+
<h2 id="label-General+Notes">General Notes<span><a href="#label-General+Notes">¶</a> <a href="#top">↑</a></span></h2>
|
148
|
+
|
149
|
+
<pre>Subinterfaces work as well. See the test_sub.rb file under the 'test'
|
150
|
+
directory for a sample.
|
151
|
+
Since the `check_interface` and `check_class` methods are meant to be
|
152
|
+
invoked for every invocation of a method, there is a runtime overhead
|
153
|
+
associated which may not be desirable in production. Hence these methods
|
154
|
+
are guarded by an environment variable RUBY_INTERFACE_TYPECHECK. Unless
|
155
|
+
this variable is set to 1, the check methods are defined as empty methods.</pre>
|
156
|
+
|
157
|
+
<h2 id="label-Runtime+performance+of+check+methods">Runtime performance of check methods<span><a href="#label-Runtime+performance+of+check+methods">¶</a> <a href="#top">↑</a></span></h2>
|
158
|
+
|
159
|
+
<pre>On a Macbook Pro 2.4 GHz Intel Core i5 machine, adding a check
|
160
|
+
method costs about 1 second for every million calls. YMMV. It is advisable
|
161
|
+
to benchmark for your code to determine if the overhead is acceptable in
|
162
|
+
your enviroment.</pre>
|
163
|
+
|
164
|
+
<h2 id="label-Developer-27s+Notes">Developer's Notes<span><a href="#label-Developer-27s+Notes">¶</a> <a href="#top">↑</a></span></h2>
|
165
|
+
|
166
|
+
<pre>A discussion on IRC with Mauricio Fernandez got us talking about traits.
|
167
|
+
During that discussion I remembered a blog entry by David Naseby. I
|
168
|
+
revisited his blog entry and took a closer look:
|
169
|
+
|
170
|
+
http://ruby-naseby.blogspot.com/2008/11/traits-in-ruby.html
|
171
|
+
|
172
|
+
Keep in mind that I also happened to be thinking about Java at the moment
|
173
|
+
because of a recent job switch that involved coding in Java. I was also
|
174
|
+
trying to figure out what the purpose of interfaces were.
|
175
|
+
|
176
|
+
As I read the first page of David Naseby's article I realized that,
|
177
|
+
whether intended or not, he had implemented a rudimentary form of interfaces
|
178
|
+
for Ruby. When I discovered this, I talked about it some more with Mauricio
|
179
|
+
and he and I (mostly him) fleshed out the rest of the module, including some
|
180
|
+
syntax improvements. The result is syntax and functionality that is nearly
|
181
|
+
identical to Java.
|
182
|
+
|
183
|
+
I should note that, although I am listed as the author, this was mostly the
|
184
|
+
combined work of David Naseby and Mauricio Fernandez. I just happened to be
|
185
|
+
the guy that put it all together.</pre>
|
186
|
+
|
187
|
+
<h2 id="label-Acknowledgements">Acknowledgements<span><a href="#label-Acknowledgements">¶</a> <a href="#top">↑</a></span></h2>
|
188
|
+
|
189
|
+
<pre>This module was largely inspired and somewhat copied from a post by
|
190
|
+
David Naseby (see URL above). It was subsequently modified almost entirely
|
191
|
+
by Mauricio Fernandez through a series of discussions on IRC.</pre>
|
192
|
+
|
193
|
+
<h2 id="label-Copyright">Copyright<span><a href="#label-Copyright">¶</a> <a href="#top">↑</a></span></h2>
|
194
|
+
|
195
|
+
<pre>(C) 2004-2016 Daniel J. Berger
|
196
|
+
All rights reserved.</pre>
|
197
|
+
|
198
|
+
<h2 id="label-Warranty">Warranty<span><a href="#label-Warranty">¶</a> <a href="#top">↑</a></span></h2>
|
199
|
+
|
200
|
+
<pre>This package is provided "as is" and without any express or
|
201
|
+
implied warranties, including, without limitation, the implied
|
202
|
+
warranties of merchantability and fitness for a particular purpose.</pre>
|
203
|
+
|
204
|
+
<h2 id="label-License">License<span><a href="#label-License">¶</a> <a href="#top">↑</a></span></h2>
|
205
|
+
|
206
|
+
<pre class="ruby"><span class="ruby-constant">Artistic</span> <span class="ruby-value">2.0</span>
|
207
|
+
</pre>
|
208
|
+
|
209
|
+
<h2 id="label-Author">Author<span><a href="#label-Author">¶</a> <a href="#top">↑</a></span></h2>
|
210
|
+
|
211
|
+
<pre class="ruby"><span class="ruby-constant">Daniel</span> <span class="ruby-constant">J</span>. <span class="ruby-constant">Berger</span>
|
212
|
+
</pre>
|
213
|
+
</main>
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
<footer id="validator-badges" role="contentinfo">
|
218
|
+
<p><a href="http://validator.w3.org/check/referer">Validate</a>
|
219
|
+
<p>Generated by <a href="http://docs.seattlerb.org/rdoc/">RDoc</a> 4.2.0.
|
220
|
+
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
|
221
|
+
</footer>
|
222
|
+
|