-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
456 lines (328 loc) · 12.7 KB
/
index.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Ruby Metaprogramming</title>
<meta name="keywords" content="ruby metaprogramming,ruby programming,metaprogramming" />
<meta name="description" content="RubyLearning.org" />
<link rel="icon" type="image/ico" href="http://rubylearning.com/images/favicon.ico" />
<link rel="stylesheet" href="html/static/style.css" type="text/css" media="screen" />
<script type="text/javascript" src="html/static/code_highlighter.js"></script>
<script type="text/javascript" src="html/static/code_highlighter_ruby.js"></script>
<base href="http://ruby-metaprogramming.rubylearning.com/" />
</head>
<body>
<div>
<h1>Metaprogramming in Ruby</h1>
<p><br /><br /></p>
<h2>First Step on Ruby Metaprogramming</h2>
<hr />
<h2>First Week</h2>
<p>Let's start learning Ruby Metaprogramming!</p>
<p>Please refer our <a href="http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_1.html">accompanying study notes and examples</a>.</p>
<p><br /><br /></p>
<h2>Study 0</h2>
<p>1st, review the built-in, read-only variable <b>self</b>. Read the following articles:</p>
<ul>
<li><a href="http://rubylearning.com/satishtalim/ruby_self.html">self</a> - The current/default object</li>
<li><p><a href="../html/self.html">self</a> - Quote from Programming Ruby 1.9</p></li>
<li><p><a href="../html/when_does_self_change.html">When does self change?</a> - Quote from The Ruby Object Model</p></li>
</ul>
<p>2nd, review <b>singleton class</b>. Read the following article:</p>
<ul>
<li><a href="http://pmade.com/articles/2008/ruby-singleton">Understanding Ruby Singleton Classes</a></li>
</ul>
<p>3rd, review the <b>scope of variables</b>. Read the following article:</p>
<ul>
<li><a href="http://www.techotopia.com/index.php/Ruby_Variable_Scope">Ruby Variable Scope</a></li>
</ul>
<p><br /></p>
<h2>Study 1</h2>
<p>To learn about the following methods read <a href="http://www.sapphiresteel.com/The-Book-Of-Ruby">The Book of Ruby</a>, Chapter 20: Dynamic Programming.</p>
<ul>
<li>eval</li>
<li>instance_eval</li>
<li>class_eval (aka: module_eval)</li>
<li>class_variable_set</li>
<li>class_variable_get</li>
<li>class_variables (Try it out: instance_variables)</li>
<li>instance_variable_set (Try it out: instance_variable_get)</li>
<li>define_method</li>
<li>const_set</li>
<li>const_get (Try it out: constants)</li>
<li>Class.new (Try it out: Struct.new)</li>
<li>binding (Try it out: lambda)</li>
<li>send (Try it out: method)</li>
<li>remove_method</li>
<li>undef_method</li>
<li>method_missing</li>
</ul>
<p><br /></p>
<h2>Study 2</h2>
<p>Read _why's hacking, <a href="../html/seeingMetaclassesClearly.html">Seeing Metaclasses Clearly</a> to learn about the following singleton class (metaclass)</p>
<ul>
<li>class << self; self; end</li>
</ul>
<p><br /></p>
<h2>Study 3</h2>
<p>Read Marc-Andre Cournoyer's blog, <a href="http://macournoyer.wordpress.com/2007/07/06/extending-your-include-knowledge-of-ruby/">Extending your include knowledge of Ruby</a> to learn about the following methods.
Also read Ruby-Doc Core API: <a href="http://www.ruby-doc.org/core/classes/Module.html#M001683">Module#included</a> and <a href="http://www.ruby-doc.org/core/classes/Module.html#M001660">Module#extended</a></p>
<ul>
<li>include</li>
<li>extend</li>
<li>included</li>
<li>extended</li>
</ul>
<p><br /></p>
<h2>Before Exercises</h2>
<p>Watch the following presentation, <a href="http://www.rubyinside.com/scotland-on-rails-presentations-now-online-27-awesome-videos-1799.html">the Scotland on Rails conference 2009</a>, by Dave Thomas.</p>
<p><a href="http://scotland-on-rails.s3.amazonaws.com/2A04_DaveThomas-SOR.mp4">The Ruby Object Model</a></p>
<p><br /></p>
<h2>Exercises</h2>
<p>Try to do the following exercises. Let's discuss all these exercises in the relevant thread in the First Week Forum.</p>
<ul>
<li><a href="../html/Exercise_1.html">Exercise 1</a>: Get the values from outside the class.</li>
<li><a href="../html/Exercise_2.html">Exercise 2</a>: Add your code to display 'I like metaprogramming!'</li>
<li><a href="../html/Exercise_3.html">Exercise 3</a>: Show lots of ways to define singleton method.</li>
<li><a href="../html/Exercise_4.html">Exercise 4</a>: Glance into Ruby inside with binding method.</li>
<li><a href="../html/Exercise_5.html">Exercise 5</a>: Define the class without <code class="ruby">class</code> and <code class="ruby">def</code>.</li>
</ul>
<p><br /></p>
<h2>Watch the video</h2>
<p>Watch the Dave Thomas's presentation about Metaprogramming.</p>
<p><a href="http://www.infoq.com/presentations/metaprogramming-ruby">MetaProgramming - Extending Ruby for Fun and Profit</a></p>
<p>Understand these concepts:</p>
<ul>
<li>Classes are open</li>
<li>Definitions are active</li>
<li>All method calls have a receiver</li>
<li>Classes are objects</li>
</ul>
<p><br /></p>
<h2>Cheat Sheet</h2>
<ul>
<li><a href="../html/cheat_sheet.html">Cheat Sheet</a>: a list of Ruby metaprogramming techniques</li>
</ul>
<p><br /><br /></p>
<hr/>
<h2>Second Week</h2>
<p><br /></p>
<p>Well, let's practice how to write a tiny app with Ruby Metaprogramming techniques.</p>
<p><b>Note</b>: If you have an idea in your mind. Feel free to please show us and try to do that.</p>
<p><br /></p>
<h2>Assignment 1</h2>
<p>Define class Dog.</p>
<p><br /></p>
<h2>Step 1</h2>
<p>There are three dogs named Lassie, Fido and Stimpy.<br />
Look at <a href="../html/Dog_Step1.html">dog_game.rb</a>. Expected output is the following:</p>
<pre><code class="ruby">"Lassie is dancing"
"Lassie is a smelly doggy!"
"Lassie finds this hilarious!"
"Fido doesn't understand dance"
"Fido is a smelly doggy!"
"Fido doesn't understand laugh"
"Stimpy is dancing"
"Stimpy doesn't understand poo"
"Stimpy doesn't understand laugh"
</code></pre>
<p>Create <b>dog.rb</b> stored the class Dog.</p>
<p><strong>Hints:</strong></p>
<ul>
<li>class Dog has three methods: initialize, can, method_missing</li>
<li>may be useful to define the static data like this:
MSGS = {:dance => 'is dancing', :poo => 'is a smelly doggy!', :laugh => 'finds this hilarious!'}</li>
</ul>
<p><br /></p>
<h2>Step 2</h2>
<p>Challenge: Improve a little bit. </p>
<p>Look at <a href="../html/Dog_Step2.html">dog_game.rb</a>. Expected output is the following:</p>
<pre><code class="ruby">"Lassie is dancing"
"Lassie is a smelly doggy!"
"Lassie finds this hilarious!"
"Fido doesn't understand dance"
"Fido is smelly."
"Fido doesn't understand laugh"
"Stimpy is dancing"
"Stimpy doesn't understand poo"
"Stimpy doesn't understand laugh"
"Stimpy cried AHHHH"
</code></pre>
<p>Let's improve <b>dog.rb</b>.</p>
<p><b>Hints:</b></p>
<ul>
<li>use <b>can</b> method <b>with block</b> like this: <b>stimpy.can(:cry){"#{name} cried AHHHH"}</b></li>
<li>define the <b>name</b> method</li>
</ul>
<p><br /></p>
<p><br /></p>
<h2>Assignment 2</h2>
<p>Try to write your own <b>alias_method_chain()</b>.</p>
<p><br /></p>
<h2>Simple Example</h2>
<p>Look at this simple example from <a href="http://www.pragprog.com/titles/ppmetr/metaprogramming-ruby">Metaprogramming Ruby</a>.</p>
<pre><code class="ruby"># simple1.rb
class MyClass
def greet
puts "Hello!"
end
end
MyClass.new.greet # => Hello!
</code></pre>
<p>Now suppose you want to wrap logging behavior around the above <b>greet()</b>.
You can do without editing original code!
Look at this:</p>
<pre><code class="ruby"># simple2.rb
require 'simple1'
class MyClass
def greet_with_log
puts "Calling method..."
puts "Hello!"
puts "...Method called"
end
alias_method :greeting_with_log, :greet
alias_method :greet, :greet_with_log
end
MyClass.new.greet
# => Hello!
# Calling method...
# Hello!
# ...Method called
</code></pre>
<p><br /></p>
<h2>Generic Metaprogramming Method</h2>
<p>Instead of duplicating these aliases all around, let's provide <b>alias_method_chain()</b>.</p>
<p>There is the original code <b>rubyist.rb</b>.</p>
<pre><code class="ruby"># rubyist.rb
class Rubyist
def initialize name
@name = name
@count = 0
end
def say!
puts 'hello'
end
end
</code></pre>
<p><br /></p>
<p>I'd like to add the count feature without editing the original file. </p>
<p>The usage is like this:</p>
<pre><code class="ruby"># test_snippet.rb
require 'rubyist'
satish = Rubyist.new('Satish')
3.times{satish.say!}
puts '-' * 20
require 'rubyist_with_count'
3.times{satish.say!}
</code></pre>
<p><br /></p>
<p>The expected output is this:</p>
<pre>
hello
hello
hello
--------------------
***called alias_method_chain***
aliased_target is: say
punctuation is: !
Satish(1) starts greeting...
hello
Satish(1) finished greeting...
Satish(2) starts greeting...
hello
Satish(2) finished greeting...
Satish(3) starts greeting...
hello
Satish(3) finished greeting...
</pre>
<h2>Game</h2>
<p>I wrote the following code. Could you please complete it? ;-)</p>
<p><br /></p>
<p><b>aliasing.rb</b></p>
<pre><code class="ruby"># aliasing.rb
module RubyLearning
module Module
def alias_method_chain(target, feature)
# write your code here :
#
# 1. Strip out the final exclamation mark or question mark or equal mark
# from the name of the method, to put it at the end of the new aliases.
#
# 2. If block given, it can pass the aliased method name and punctuation
# to the block.
#
# 3. Alias the methods operation_with_feature() and operation_without_feature().
#
# 4. Set the same visibility as original method - private or public or protected
#
end
end
end
</code></pre>
<p><br /></p>
<p><b>rubyist_with_count.rb</b></p>
<pre><code class="ruby"># rubyist_with_count.rb
require 'rubyist'
require 'aliasing'
class Rubyist
extend RubyLearning::Module
def say_with_count!
# write your code here to show the expected output.
end
# write your code here to show the expected output.
# like this:
#
# alias_method_chain args do |variables|
# bla-bla-bla
# end
end
</code></pre>
<p><b>Okay, let's discuss your code in the relevant thread in the Second Week Forum. :-D</b></p>
<p><br /></p>
<h1>For More Study</h1>
<p><br /></p>
<ul>
<li><p><a href="http://github.com/ashbb/ruby_metaprogramming_study_note/tree/master">Ruby Metaprogramming Study Note</a> Try to hack the Sample Apps!</p></li>
<li><p><a href="http://media.pragprog.com/titles/ppmetr/spells.pdf">Spell Book</a> The excerpt from Metaprogramming Ruby. Collection of Metaprogramming-related small snippets. Useful as a quick reference. For free!</p></li>
</ul>
<p><br /></p>
<h2>Interesting Articles</h2>
<ul>
<li><a href="http://technicalpickles.com/posts/using-method_missing-and-respond_to-to-create-dynamic-methods">Using method<em>missing and respond</em>to? to create dynamic methods</a></li>
<li><a href="http://ola-bini.blogspot.com/search/label/metaprogramming">Ola Bini's blogs on Meta programming</a></li>
<li><a href="http://www.rootr.net/rubyfaq.html">The Ruby Language FAQ</a></li>
<li><a href="http://www.ruby-forum.com/topic/190407">Trying to define a 'class' without using 'class' sentence</a></li>
<li><a href="http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/">Ruby Mixin Tutorial</a></li>
<li><a href="http://www.infoq.com/articles/eval-options-in-ruby">Evaluation Options in Ruby</a></li>
</ul>
<p><br /></p>
<h2>Recommended Book</h2>
<p>I'd like to highly recommend this book. ;-)</p>
<ul>
<li><a href="http://www.pragprog.com/titles/ppmetr/metaprogramming-ruby">Metaprogramming Ruby</a> by <a href="http://rubylearning.com/blog/2009/07/01/interview-author-paolo-perrotta/">Paolo Perrotta</a></li>
</ul>
<p><br /><br /></p>
<hr/>
<p>2009.12.20 ashbb</p>
<hr />
<h2>P.S.</h2>
<p>Thank you for reading this Ruby Metaprogramming learning guide.<br />
If you are curious, join <strong>the Ruby Metaprogramming course</strong> on <a href="http://www.rubylearning.org/class/">RubyLearning</a>. Details are <a href="http://rubylearning.com/blog/2009/10/10/ruby-metaprogramming-new-batch-announced/">here</a>.<br />
See you! :-D</p>
<p class="copyright">This page was last updated on 20th Dec. 2009.</p>
</div>
<!-- Hit Tail (Invisible code) -->
<script src="http://13043.hittail.com/mlt.js" type="text/javascript"></script>
<!-- Hit Tail (Invisible code) -->
<!-- Google Analytics code -->
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-59044-10");
pageTracker._trackPageview();
</script>
<!-- Google Analytics code ends -->
</body>
</html>