Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.programming.html
More file actions
Latest commit
388 lines (283 loc) · 26.8 KB
/
Copy pathfile.programming.html
File metadata and controls
388 lines (283 loc) · 26.8 KB
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<metahttp-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>
File: RubyCocoa Programming
— RubyCocoa Documentation
</title>
<linkrel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
<linkrel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
<scripttype="text/javascript" charset="utf-8">
hasFrames=window.top.frames.main ? true : false;
relpath='';
framesUrl="frames.html#!file.programming.html";
</script>
<scripttype="text/javascript" charset="utf-8" src="js/jquery.js"></script>
<scripttype="text/javascript" charset="utf-8" src="js/app.js"></script>
</head>
<body>
<divid="header">
<divid="menu">
<ahref="_index.html">Index</a> »
<spanclass="title">File: RubyCocoa Programming</span>
<divclass="noframes"><spanclass="title">(</span><ahref="." target="_top">no frames</a><spanclass="title">)</span></div>
</div>
<divid="search">
<aclass="full_list_link" id="class_list_link"
href="class_list.html">
Class List
</a>
<aclass="full_list_link" id="method_list_link"
href="method_list.html">
Method List
</a>
<aclass="full_list_link" id="file_list_link"
href="file_list.html">
File List
</a>
</div>
<divclass="clear"></div>
</div>
<iframeid="search_frame"></iframe>
<divid="content"><divid='filecontents'>
<h1id="label-RubyCocoa+Programming">RubyCocoa Programming</h1>
<h2id="label-irb+-+Interactive+Ruby">irb - Interactive Ruby</h2>
<p>You may want to use <code>irb</code> to try the script snippets in this
document. <code>irb</code> is a command that lets you use a Ruby
interpreter interactively from the command line. You can start an
interactive session with RubyCocoa using the following command:</p>
<preclass="code ruby"><codeclass="ruby">% irb -r osx/cocoa</code></pre>
<p>(NOTE) In Mac OS X 10.9, use
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/irb.</p>
<h2id="label-Load+libraries">Load libraries</h2>
<preclass="code ruby"><codeclass="ruby"><spanclass='id identifier rubyid_require'>require</span><spanclass='tstring'><spanclass='tstring_beg'>'</span><spanclass='tstring_content'>osx/cocoa</span><spanclass='tstring_end'>'</span></span><spanclass='comment'># classes defined in Foundation and AppKit.
</span></code></pre>
<p>or</p>
<preclass="code ruby"><codeclass="ruby"><spanclass='id identifier rubyid_require'>require</span><spanclass='tstring'><spanclass='tstring_beg'>'</span><spanclass='tstring_content'>osx/foundation</span><spanclass='tstring_end'>'</span></span><spanclass='comment'># classes defined in Foundation
</span><spanclass='id identifier rubyid_require'>require</span><spanclass='tstring'><spanclass='tstring_beg'>'</span><spanclass='tstring_content'>osx/appkit</span><spanclass='tstring_end'>'</span></span><spanclass='comment'># classes defined in AppKit
</span></code></pre>
<h2id="label-A+first+example+with+sensory+appeal">A first example with sensory appeal</h2>
<p>Let's start with a simple example that will delight your senses – this
script will play a sound. Try this with [irb]:</p>
<preclass="code ruby"><codeclass="ruby">include OSX
files = `ls /System/Library/Sounds/*.aiff`.split
NSSound.alloc.initWithContentsOfFile_byReference (files[0], true).play
NSSound.alloc.initWithContentsOfFile_byReference (files[1], true).play
NSSound.alloc.initWithContentsOfFile_byReference (files[2], true).play</code></pre>
<h2id="label-Cocoa+classes+are+Ruby+classes">Cocoa classes are Ruby classes</h2>
<p>In the following code examples, the comments starting with <code>#
=></code> show the string Ruby will output when it executes that line of
code.</p>
<preclass="code ruby"><codeclass="ruby"><spanclass='id identifier rubyid_p'>p</span><spanclass='const'>OSX</span><spanclass='op'>::</span><spanclass='const'>NSObject</span><spanclass='comment'># => OSX::NSObject
</span><spanclass='id identifier rubyid_nsstr'>nsstr</span><spanclass='op'>=</span><spanclass='const'>OSX</span><spanclass='op'>::</span><spanclass='const'>NSObject</span><spanclass='period'>.</span><spanclass='id identifier rubyid_description'>description</span>
<spanclass='id identifier rubyid_p'>p</span><spanclass='id identifier rubyid_nsstr'>nsstr</span><spanclass='comment'># => #<OSX::OCObject:0x5194e8 class='NSCFString' id=A97910>
</span><spanclass='id identifier rubyid_nsobj'>nsobj</span><spanclass='op'>=</span><spanclass='const'>OSX</span><spanclass='op'>::</span><spanclass='const'>NSObject</span><spanclass='period'>.</span><spanclass='id identifier rubyid_alloc'>alloc</span><spanclass='period'>.</span><spanclass='id identifier rubyid_init'>init</span>
<spanclass='id identifier rubyid_p'>p</span><spanclass='id identifier rubyid_nsobj'>nsobj</span><spanclass='comment'># => #<OSX::NSObject:0x51f5b4 class='NSObject' id=976D90>
</span></code></pre>
<p>In RubyCocoa, a Cocoa class is defined as a Ruby class under the
<code>OSX</code> module. A Cocoa class is a Ruby class and behaves as a
Cocoa object.</p>
<h2id="label-Creating+a+Cocoa+object">Creating a Cocoa object</h2>
<p>The normal Cocoa methods are used for creation of Cocoa objects.</p>
<preclass="code ruby"><codeclass="ruby"><spanclass='id identifier rubyid_obj'>obj</span><spanclass='op'>=</span><spanclass='const'>OSX</span><spanclass='op'>::</span><spanclass='const'>NSObject</span><spanclass='period'>.</span><spanclass='id identifier rubyid_alloc'>alloc</span><spanclass='period'>.</span><spanclass='id identifier rubyid_init'>init</span>
<spanclass='id identifier rubyid_str'>str</span><spanclass='op'>=</span><spanclass='const'>OSX</span><spanclass='op'>::</span><spanclass='const'>NSString</span><spanclass='period'>.</span><spanclass='id identifier rubyid_stringWithString'>stringWithString</span><spanclass='tstring'><spanclass='tstring_beg'>"</span><spanclass='tstring_content'>hello</span><spanclass='tstring_end'>"</span></span>
<spanclass='id identifier rubyid_str'>str</span><spanclass='op'>=</span><spanclass='const'>OSX</span><spanclass='op'>::</span><spanclass='const'>NSString</span><spanclass='period'>.</span><spanclass='id identifier rubyid_alloc'>alloc</span><spanclass='period'>.</span><spanclass='id identifier rubyid_initWithString'>initWithString</span><spanclass='tstring'><spanclass='tstring_beg'>"</span><spanclass='tstring_content'>world</span><spanclass='tstring_end'>"</span></span>
</code></pre>
<p>Inside RubyCocoa, the created Cocoa object is wrapped in the object of a
class called <code>OSX::ObjcID</code>. Usually, you don't need to be
conscious of the existence of an <code>OSX::ObjcID</code> class.</p>
<h2id="label-Ownership+and+memory+management">Ownership and memory management</h2>
<p>The instance of <code>OSX::ObjcID</code> is the real owner of the Cocoa
object which <code>self</code> has wrapped. Ownership is automatically lost
when the instance of <code>OSX::ObjcID</code> is cleaned by Ruby's
garbage collection. Therefore, it is not necessary to worry about memory
management issues such as ownership in RubyCocoa.</p>
<preclass="code ruby"><codeclass="ruby"><spanclass='id identifier rubyid_str'>str</span><spanclass='op'>=</span><spanclass='const'>OSX</span><spanclass='op'>::</span><spanclass='const'>NSString</span><spanclass='period'>.</span><spanclass='id identifier rubyid_stringWithString'>stringWithString</span><spanclass='tstring'><spanclass='tstring_beg'>"</span><spanclass='tstring_content'>hello</span><spanclass='tstring_end'>"</span></span>
<spanclass='id identifier rubyid_str'>str</span><spanclass='op'>=</span><spanclass='const'>OSX</span><spanclass='op'>::</span><spanclass='const'>NSString</span><spanclass='period'>.</span><spanclass='id identifier rubyid_alloc'>alloc</span><spanclass='period'>.</span><spanclass='id identifier rubyid_initWithString'>initWithString</span><spanclass='tstring'><spanclass='tstring_beg'>"</span><spanclass='tstring_content'>world</span><spanclass='tstring_end'>"</span></span>
</code></pre>
<p>Although in Objective-C the two lines above differ as to whether ownership
is generated or delegated, in RubyCocoa there is no need to be conscious of
ownership; the difference between the two techniques shown above is not
such an important issue. In principle, it is not necessary to call methods
such as <code>release</code>, <code>autorelease</code>, and
<code>retain</code>, and you do not need to create
<code>NSAutoreleasePool</code>s.</p>
<ul><li>
<p>Use Cocoa methods to create Cocoa objects.</p>
</li><li>
<p>Don't worry about ownership and memory management.</p>
</li></ul>
<h2id="label-Return+value+of+methods">Return value of methods</h2>
<preclass="code ruby"><codeclass="ruby"><spanclass='id identifier rubyid_nstr'>nstr</span><spanclass='op'>=</span><spanclass='const'>OSX</span><spanclass='op'>::</span><spanclass='const'>NSString</span><spanclass='period'>.</span><spanclass='id identifier rubyid_description'>description</span>
<spanclass='id identifier rubyid_p'>p</span><spanclass='id identifier rubyid_nstr'>nstr</span><spanclass='comment'># => #<OCObject:0x7233e class='NSCFString' id=687610>
</span><spanclass='id identifier rubyid_p'>p</span><spanclass='id identifier rubyid_nstr'>nstr</span><spanclass='period'>.</span><spanclass='id identifier rubyid_to_s'>to_s</span><spanclass='comment'># => "NSString"
</span>
<spanclass='id identifier rubyid_nstr'>nstr</span><spanclass='op'>=</span><spanclass='const'>OSX</span><spanclass='op'>::</span><spanclass='const'>NSString</span><spanclass='period'>.</span><spanclass='id identifier rubyid_stringWithString'>stringWithString</span><spanclass='tstring'><spanclass='tstring_beg'>"</span><spanclass='tstring_content'>Hello World !</span><spanclass='tstring_end'>"</span></span>
<spanclass='id identifier rubyid_p'>p</span><spanclass='id identifier rubyid_nstr'>nstr</span><spanclass='comment'># => #<OCObject:0x71970 class='NSCFString' id=688E90>
</span><spanclass='id identifier rubyid_p'>p</span><spanclass='id identifier rubyid_nstr'>nstr</span><spanclass='period'>.</span><spanclass='id identifier rubyid_to_s'>to_s</span><spanclass='comment'># => "Hello World !"
</span>
<spanclass='id identifier rubyid_nstr'>nstr</span><spanclass='op'>=</span><spanclass='const'>OSX</span><spanclass='op'>::</span><spanclass='const'>NSString</span><spanclass='period'>.</span><spanclass='id identifier rubyid_stringWithString'>stringWithString</span><spanclass='lparen'>(</span><spanclass='backtick'>`</span><spanclass='tstring_content'>pwd</span><spanclass='tstring_end'>`</span></span><spanclass='period'>.</span><spanclass='id identifier rubyid_chop'>chop</span><spanclass='rparen'>)</span>
<spanclass='id identifier rubyid_nary'>nary</span><spanclass='op'>=</span><spanclass='id identifier rubyid_nstr'>nstr</span><spanclass='period'>.</span><spanclass='id identifier rubyid_pathComponents'>pathComponents</span>
<spanclass='id identifier rubyid_p'>p</span><spanclass='id identifier rubyid_nary'>nary</span><spanclass='comment'># => #<OCObject:0x6bb2e class='NSCFArray' id=3C0150>
</span>
<spanclass='id identifier rubyid_ary'>ary</span><spanclass='op'>=</span><spanclass='id identifier rubyid_nary'>nary</span><spanclass='period'>.</span><spanclass='id identifier rubyid_to_a'>to_a</span>
<spanclass='id identifier rubyid_p'>p</span><spanclass='id identifier rubyid_ary'>ary</span><spanclass='comment'># => [#<OCObject:0x6a9b8 class='NSCFString' id=3C2B50>,...]
</span>
<spanclass='id identifier rubyid_ary'>ary</span><spanclass='period'>.</span><spanclass='id identifier rubyid_map!'>map!</span><spanclass='lbrace'>{</span><spanclass='op'>|</span><spanclass='id identifier rubyid_i'>i</span><spanclass='op'>|</span><spanclass='id identifier rubyid_i'>i</span><spanclass='period'>.</span><spanclass='id identifier rubyid_to_s'>to_s</span><spanclass='rbrace'>}</span>
<spanclass='id identifier rubyid_p'>p</span><spanclass='id identifier rubyid_ary'>ary</span><spanclass='comment'># => ["/", "Users", "hisa", "src", "ruby", "osxobjc"]
</span></code></pre>
<p>In RubyCocoa, methods that return Objective-C objects such as
<code>NSString</code> and <code>NSArray</code> return Cocoa objects, as you
might have guessed from these examples. The return value is not
automatically converted to the corresponding Ruby class
(<code>String</code>, for example). For <code>NSString</code> and
<code>NSArray</code>, <code>to_s</code> and <code>to_a</code> are defined
and can be used.</p>
<h2id="label-Representing+Objective-C+message+selectors">Representing Objective-C message selectors</h2>
<preclass="code ruby"><codeclass="ruby"><spanclass='comment'># play system sounds (2)
</span><spanclass='id identifier rubyid_sndfiles'>sndfiles</span><spanclass='period'>.</span><spanclass='id identifier rubyid_each'>each</span><spanclass='kw'>do</span><spanclass='op'>|</span><spanclass='id identifier rubyid_path'>path</span><spanclass='op'>|</span>
<spanclass='id identifier rubyid_snd'>snd</span><spanclass='op'>=</span><spanclass='const'>OSX</span><spanclass='op'>::</span><spanclass='const'>NSSound</span><spanclass='period'>.</span><spanclass='id identifier rubyid_alloc'>alloc</span><spanclass='period'>.</span><spanclass='id identifier rubyid_initWithContentsOfFile'>initWithContentsOfFile</span><spanclass='lparen'>(</span><spanclass='id identifier rubyid_path'>path</span><spanclass='comma'>,</span><spanclass='symbol'>:byReference</span><spanclass='comma'>,</span><spanclass='kw'>true</span><spanclass='rparen'>)</span>
<spanclass='id identifier rubyid_snd'>snd</span><spanclass='period'>.</span><spanclass='id identifier rubyid_play'>play</span>
<spanclass='id identifier rubyid_sleep'>sleep</span><spanclass='float'>0.25</span><spanclass='kw'>while</span><spanclass='id identifier rubyid_snd'>snd</span><spanclass='period'>.</span><spanclass='id identifier rubyid_isPlaying?'>isPlaying?</span>
<spanclass='kw'>end</span>
</code></pre>
<p>This is another version of “playing system sounds”. This shows the other
way Objective-C message selectors can be represented in the Ruby world.</p>
<p>In Objective-C:</p>
<preclass="code ruby"><codeclass="ruby">[obj hogeAt: a0 withParamA: a1 withParamB: a2]</code></pre>
<p>RubyCocoa provides several ways to specify message selectors. The simplest
way is to substitute “<code>:</code>” with “<code>_</code>”.</p>
<preclass="code ruby"><codeclass="ruby">obj.hogeAt_withParamA_withParamB_ (a0, a1, a2)</code></pre>
<p>But because this looks awkward, you can omit the last underscore.</p>
<preclass="code ruby"><codeclass="ruby">obj.hogeAt_withParamA_withParamB (a0, a1, a2)</code></pre>
<p>When the method name is very long, the relationship between the message
selector keyword and each argument is unclear. In order to improve this:</p>
<preclass="code ruby"><codeclass="ruby">obj.hogeAt (a0, :withParamA, a1, :withParamB, a2)</code></pre>
<p>For Cocoa methods that return the <code>BOOL</code> type (predicate
method), use the method name suffix “?” to return a Ruby boolean. If this
suffix is omitted, the method will return the value <code>0</code> (NO) or
<code>1</code> (YES). These values behave as <code>true</code> in the Ruby
world, so you will get unexpected results.</p>
<preclass="code ruby"><codeclass="ruby"><spanclass='id identifier rubyid_nary'>nary</span><spanclass='op'>=</span><spanclass='const'>OSX</span><spanclass='op'>::</span><spanclass='const'>NSMutableArray</span><spanclass='period'>.</span><spanclass='id identifier rubyid_alloc'>alloc</span><spanclass='period'>.</span><spanclass='id identifier rubyid_init'>init</span>
<spanclass='id identifier rubyid_p'>p</span><spanclass='id identifier rubyid_nary'>nary</span><spanclass='period'>.</span><spanclass='id identifier rubyid_containsObject'>containsObject</span><spanclass='lparen'>(</span><spanclass='tstring'><spanclass='tstring_beg'>"</span><spanclass='tstring_content'>hoge</span><spanclass='tstring_end'>"</span></span><spanclass='rparen'>)</span><spanclass='comment'># => 0
</span><spanclass='id identifier rubyid_p'>p</span><spanclass='id identifier rubyid_nary'>nary</span><spanclass='period'>.</span><spanclass='id identifier rubyid_containsObject?'>containsObject?</span><spanclass='lparen'>(</span><spanclass='tstring'><spanclass='tstring_beg'>"</span><spanclass='tstring_content'>hoge</span><spanclass='tstring_end'>"</span></span><spanclass='rparen'>)</span><spanclass='comment'># => false
</span><spanclass='id identifier rubyid_nary'>nary</span><spanclass='period'>.</span><spanclass='id identifier rubyid_addObject'>addObject</span><spanclass='lparen'>(</span><spanclass='tstring'><spanclass='tstring_beg'>"</span><spanclass='tstring_content'>hoge</span><spanclass='tstring_end'>"</span></span><spanclass='rparen'>)</span>
<spanclass='id identifier rubyid_p'>p</span><spanclass='id identifier rubyid_nary'>nary</span><spanclass='period'>.</span><spanclass='id identifier rubyid_containsObject'>containsObject</span><spanclass='lparen'>(</span><spanclass='tstring'><spanclass='tstring_beg'>"</span><spanclass='tstring_content'>hoge</span><spanclass='tstring_end'>"</span></span><spanclass='rparen'>)</span><spanclass='comment'># => 1
</span><spanclass='id identifier rubyid_p'>p</span><spanclass='id identifier rubyid_nary'>nary</span><spanclass='period'>.</span><spanclass='id identifier rubyid_containsObject?'>containsObject?</span><spanclass='lparen'>(</span><spanclass='tstring'><spanclass='tstring_beg'>"</span><spanclass='tstring_content'>hoge</span><spanclass='tstring_end'>"</span></span><spanclass='rparen'>)</span><spanclass='comment'># => true
</span></code></pre>
<h2id="label-Convert+Ruby+object+method+arguments+when+possible">Convert Ruby object method arguments when possible</h2>
<p>It seems to be usual containsObject of the top and, in case of method to
catch Objective-C object as a value of argument, tries conversion even if
it just hands Ruby object so long as it is possible.</p>
<h2id="label-Handling+method+name+conflicts">Handling method name conflicts</h2>
<preclass="code ruby"><codeclass="ruby"><spanclass='id identifier rubyid_klass'>klass</span><spanclass='op'>=</span><spanclass='const'>OSX</span><spanclass='op'>::</span><spanclass='const'>NSObject</span><spanclass='period'>.</span><spanclass='id identifier rubyid_class'>class</span>
<spanclass='id identifier rubyid_p'>p</span><spanclass='id identifier rubyid_klass'>klass</span><spanclass='comment'># => OSX::OCObject
</span><spanclass='id identifier rubyid_klass'>klass</span><spanclass='op'>=</span><spanclass='const'>OSX</span><spanclass='op'>::</span><spanclass='const'>NSObject</span><spanclass='period'>.</span><spanclass='id identifier rubyid_oc_class'>oc_class</span>
<spanclass='id identifier rubyid_p'>p</span><spanclass='id identifier rubyid_klass'>klass</span><spanclass='comment'># => #<OCObject:0x82f22 class='NSObject' id=80819B0C>
</span></code></pre>
<p>When the same method name exists in Ruby and Objective-C, like in the case
of <code>Object#class</code>, prefix the method name with
“<code>oc_</code>”.</p>
<h2id="label-Inheriting+from+Cocoa">Inheriting from Cocoa</h2>
<p>So far, we've discussed existing Cocoa classes and their instances.
From this point, we'll discuss the definition and instantiation of
derived class of Cocoa, which is also needed when writing RubyCocoa
applications. Since the implementation of derived class mechanism for
RubyCocoa is a little tricky, there are some restrictions and
peculiarities.</p>
<h2id="label-Defining+a+Cocoa-inherited+class">Defining a Cocoa-inherited class</h2>
<p>The class of the Cocoa objects set up in the GUI definition file (nib file)
created by Interface Builder is defined as an inherited class (after
0.2.0). For example, the Controller of the MVC model as described in many
Cocoa tutorials is defined in Ruby like this:</p>
<preclass="code ruby"><codeclass="ruby"><spanclass='kw'>class</span><spanclass='const'>AppController</span><spanclass='op'><</span><spanclass='const'>OSX</span><spanclass='op'>::</span><spanclass='const'>NSObject</span>
<spanclass='id identifier rubyid_ib_outlets'>ib_outlets</span><spanclass='symbol'>:messageField</span>
<spanclass='kw'>def</span><spanclass='id identifier rubyid_btnClicked'>btnClicked</span><spanclass='lparen'>(</span><spanclass='id identifier rubyid_sender'>sender</span><spanclass='rparen'>)</span>
<spanclass='ivar'>@messageField</span><spanclass='period'>.</span><spanclass='id identifier rubyid_setStringValue'>setStringValue</span><spanclass='tstring'><spanclass='tstring_beg'>"</span><spanclass='tstring_content'>Merry Xmas !</span><spanclass='tstring_end'>"</span></span>
<spanclass='kw'>end</span>
<spanclass='kw'>end</span>
</code></pre>
<p>The inherited class definition of Cocoa in RubyCocoa is similarly described
to be the inherited class definition by the usual Ruby in this way.</p>
<h2id="label-Defining+Interface+Builder+outlets">Defining Interface Builder outlets</h2>
<p>The outlet set as the class in the nib file is written to be:</p>
<preclass="code ruby"><codeclass="ruby"><spanclass='id identifier rubyid_ns_outlets'>ns_outlets</span><spanclass='symbol'>:rateField</span><spanclass='comma'>,</span><spanclass='symbol'>:dollerField</span>
</code></pre>
<p>in the definition of an inherited class. In fact, <code>ns_outlets</code>
is the same as <code>Module#attr_writer</code>. Therefore, a definition can
alternatively be given this way:</p>
<preclass="code ruby"><codeclass="ruby"><spanclass='kw'>def</span><spanclass='id identifier rubyid_rateField='>rateField=</span><spanclass='lparen'>(</span><spanclass='id identifier rubyid_new_val'>new_val</span><spanclass='rparen'>)</span>
<spanclass='ivar'>@rateField</span><spanclass='op'>=</span><spanclass='id identifier rubyid_new_val'>new_val</span>
<spanclass='kw'>end</span>
</code></pre>
<p><code>ns_outlets</code> also has an alias called <code>ib_outlets</code>.</p>
<h2id="label-Overriding+a+method">Overriding a method</h2>
<p>When overriding a method defined by the parent class, it is necessary to
declare the override using <code>ns_overrides</code> (alias
<code>ib_overrides</code>).</p>
<preclass="code ruby"><codeclass="ruby">class MyCustomView < OSX::NSView
ns_overrides :drawRect_, 'mouseUp:'
def drawRect(frame)
end
...
end</code></pre>
<p>In the argument of <code>ns_overrides</code> what expressed the message
selector of Objective-C as the string or the symbol is given. However, the
notation for omitting “:” and “_” of the end explained previously cannot be
used. It is necessary to describe correctly according to the number of
arguments.</p>
<p>To invoke the superclass method in an overriding method, prefix the method
name with “<code>super_</code>”.</p>
<preclass="code ruby"><codeclass="ruby"><spanclass='kw'>class</span><spanclass='const'>MyCustomView</span><spanclass='op'><</span><spanclass='const'>OSX</span><spanclass='op'>::</span><spanclass='const'>NSView</span>
<spanclass='id identifier rubyid_ns_overrides'>ns_overrides</span><spanclass='symbol'>:drawRect_</span>
<spanclass='kw'>def</span><spanclass='id identifier rubyid_drawRect'>drawRect</span><spanclass='lparen'>(</span><spanclass='id identifier rubyid_frame'>frame</span><spanclass='rparen'>)</span>
<spanclass='id identifier rubyid_p'>p</span><spanclass='id identifier rubyid_frame'>frame</span>
<spanclass='id identifier rubyid_super_drawRect'>super_drawRect</span><spanclass='lparen'>(</span><spanclass='id identifier rubyid_frame'>frame</span><spanclass='rparen'>)</span><spanclass='comment'># invoke the implementation of NSView#drawRect
</span><spanclass='kw'>end</span>
<spanclass='kw'>end</span>
</code></pre>
<h2id="label-Instantiating+a+Cocoa-inherited+class">Instantiating a Cocoa-inherited class</h2>
<p>When an instance of a Cocoa-inherited class needs to be created in a Ruby
script, it writes like:</p>
<preclass="code ruby"><codeclass="ruby"><spanclass='const'>AppController</span><spanclass='period'>.</span><spanclass='id identifier rubyid_alloc'>alloc</span><spanclass='period'>.</span><spanclass='id identifier rubyid_init'>init</span><spanclass='comment'># use this
</span></code></pre>
<p>like the case of the existing Cocoa class. The usual Ruby idiom:</p>
<preclass="code ruby"><codeclass="ruby"><spanclass='const'>AppController</span><spanclass='period'>.</span><spanclass='id identifier rubyid_new'>new</span><spanclass='comment'># don't use this
</span></code></pre>
<p>cannot be used (it raises an exception). Although there are various
situations in this, since it becomes long, a detailed explanation is
omitted here.</p>
<p>These restrictions have deep relation in instance generation being
performed in the turn:</p>
<ul><li>
<p>alloc (in Objective-C world)</p>
</li><li>
<p>in alloc, create a Ruby object (initialize method is called here)</p>
</li></ul>
<h2id="label-Where+should+initialization+code+be+written-3F">Where should initialization code be written?</h2>
<p>In Ruby an initialization procedure is written in the
“<code>initialize</code>” method generally. But you should be careful in
doing so. When the “initialize” method is invoked, a Cocoa object in the
Objective-C space is just only given memory. And it is not initialized yet.
Therefore, in the “initialize” method, you must not invoke a method
implemented in Objective-C space. You should use only a method by Ruby at
the point.</p>
<p>If the object needs to be loaded from a nib file, initializing by the
“awakeFromNib” method is safest. Doesn't it seem that it is also
necessary to actually define the inherited class of Cocoa in most of these
cases?</p>
<p>In other cases, initialization is done in the style of Cocoa's
<code>init</code>. It is probably a good idea to write to a method with a
prefix.</p>
<p>Please do not forget to return <code>self</code> from initialization
methods.</p>
</div></div>
<divid="footer">
Generated on Sun May 31 11:56:30 2015 by
<ahref="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
0.8.7.6 (ruby-2.2.2).
</div>
</body>
</html>