-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
739 lines (713 loc) · 17.4 KB
/
index.html
File metadata and controls
739 lines (713 loc) · 17.4 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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Introspection</title>
<meta content="" name="description" />
<meta content="" name="author" />
<link href="css/reveal.css" rel="stylesheet" />
<link href="css/simple.css" rel="stylesheet" />
<link href="css/googlecode.css" rel="stylesheet" />
</head>
<body>
<div class="reveal">
<div class="slides"><section class="center" data-background="#000">
<h2 class="white">
Introspection
</h2>
</section>
<section class="center" data-background="https://m1.behance.net/rendition/modules/65216951/disp/3a5948b5e73dff1e4b791d9cf806710e.jpg">
<p class="fragment white">
I'm back!
</p>
</section>
<section class="center" data-background="https://m1.behance.net/rendition/modules/65216951/disp/3a5948b5e73dff1e4b791d9cf806710e.jpg">
<p class="white">
<3 Stanislav and Akseniya
</p>
</section>
<section class="center" data-background="https://m1.behance.net/rendition/modules/65216951/disp/3a5948b5e73dff1e4b791d9cf806710e.jpg">
<h2 class="white">
Questions
</h2>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">PI = 3.14
module X
PI = 23
end
module X::Y
p PI
end </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">PI = 3.14
module X
PI = 23
end
module X:Y
p PI
end </code></pre>
</section>
<section class="center">
<img class="inline" src="img/troll.png" />
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">(2..Float::INFINITY).first(100).select(&:prime?) </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">(2..Float::INFINITY).lazy.map { |number| number ** 2 }.select(&:odd?).to_a </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">(2..Float::INFINITY).lazy.map { |number| number ** 2 }.select(&:odd?).first(1000) </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">def order(drink, size:, syrup: 'hazelnut')
# ...
end </code></pre>
</section>
<section class="center">
<p>
What is a scope gate?
</p>
</section>
<section class="center">
<p>
Which are the keywords that introduce scope gates?
</p>
</section>
<section class="center">
<p>
Where are instance variables stored?
</p>
</section>
<section class="center">
<p>
Where are methods stored?
</p>
</section>
<section class="center">
<p>
What is a module?
</p>
</section>
<section class="center" data-background="#F00">
<h2 class="white">
Introspection
</h2>
</section>
<section class="center" data-background="#0F0">
<h2 class="white">
Instance Variables API
</h2>
</section>
<section>
<h2>
Object#instance_variable_defined?
</h2>
<p>
Returns true if the given instance variable is defined in obj
</p>
</section>
<section>
<h2>
Object#instance_variable_defined?
</h2>
<p>
String arguments are converted to symbols
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">class Foo
def initialize(a, b)
@a, @b = a, b
end
end </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">foo = Foo.new('answer', 42)
foo.instance_variable_defined? :@a #=> true
foo.instance_variable_defined? '@b' #=> true
foo.instance_variable_defined? '@c' #=> false </code></pre>
</section>
<section>
<h2>
Object#instance_variable_get
</h2>
<p>
Returns the value of the given instance variable, or nil if the instance variable is not set
</p>
</section>
<section>
<h2>
Object#instance_variable_get
</h2>
<p>
The @ part of the variable name should be included for regular instance variables
</p>
</section>
<section>
<h2>
Object#instance_variable_get
</h2>
<p>
Throws a NameError exception if the supplied symbol is not valid as an instance variable name
</p>
</section>
<section>
<h2>
Object#instance_variable_get
</h2>
<p>
String arguments are converted to symbols
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">class Foo
def initialize(a, b)
@a, @b = a, b
end
end </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">foo = Foo.new('answer', 42)
foo.instance_variable_get :@a #=> 'answer'
foo.instance_variable_get '@b' #=> 42
foo.instance_variable_get '@c' #=> nil </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">foo.instance_variable_get? 'a' #=> NameError: `a' is not allowed as an instance variable name` </code></pre>
</section>
<section>
<h2>
Object#instance_variable_set
</h2>
<p>
Sets the instance variable names by symbol to object
</p>
</section>
<section>
<h2>
Object#instance_variable_set
</h2>
<p>
Thereby frustrating the efforts of the class’s author to attempt to provide proper encapsulation
</p>
</section>
<section class="center">
<img class="inline" src="img/troll.png" />
</section>
<section>
<h2>
Object#instance_variable_set
</h2>
<p>
The variable did not have to exist prior to this call
</p>
</section>
<section>
<h2>
Object#instance_variable_set
</h2>
<p>
If the instance variable name is passed as a string, that string is converted to a symbol
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">class Foo
def initialize(a, b)
@a, @b = a, b
end
end </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">foo = Foo.new('answer', 42)
foo.instance_variable_get :@a #=> 'answer'
foo.instance_variable_get '@b' #=> 42 </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">foo = Foo.new('answer', 42)
foo.instance_variable_set @a, 'aaa' #=> 'aaa'
foo.instance_variable_set '@b', 'bbb' #=> 'bbb'
foo.instance_variable_set '@c', 'ccc' #=> 'ccc'
foo.instance_variables #=> [:a, :b, :c] </code></pre>
</section>
<section>
<h2>
Object#instance_variables
</h2>
<p>
Returns an array of instance variable names for the receiver
</p>
</section>
<section>
<h2>
Object#instance_variables
</h2>
<p>
Note that simply defining an accessor does not create the corresponding instance variable
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">class Bar
attr_reader :a, :b
end
Bar.new.instance_variables #=> [] </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">bar = Bar.new
bar.instance_variable_set @a, 'aaa' #=> 'aaa'
bar.instance_variable_set '@b', 'bbb' #=> 'bbb'
bar.instance_variables #=> [:a, :b] </code></pre>
</section>
<section class="center" data-background="#0F0">
<h2 class="white">
Class Variables API
</h2>
</section>
<section>
<h2>
Module#class_variable_defined?
</h2>
<p>
Returns true if the given class variable is defined in obj
</p>
</section>
<section>
<h2>
Module#class_variable_defined?
</h2>
<p>
String arguments are converted to symbols
</p>
</section>
<section>
<h2>
Module#class_variable_get
</h2>
<p>
Returns the value of the given class variable (or throws a NameError exception)
</p>
</section>
<section>
<h2>
Module#class_variable_get
</h2>
<p>
The @@ part of the variable name should be included for regular class variables String arguments are converted to symbols
</p>
</section>
<section>
<h2>
Module#class_variable_set
</h2>
<p>
Sets the class variable names by symbol to object
</p>
</section>
<section>
<h2>
Module#class_variable_set
</h2>
<p>
If the class variable name is passed as a string, that string is converted to a symbol
</p>
</section>
<section>
<h2>
Module#class_variables
</h2>
<p>
Returns an array of the names of class variables in mod
</p>
</section>
<section>
<h2>
Module#class_variables
</h2>
<p>
This includes the names of class variables in any included modules, unless the inherit parameter is set to false
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">class Fred
@@foo = 99
def foo
@@foo
end
end </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">Fred.class_variable_get :@@foo #=> 99
Fred.class_variable_set :@@foo, 101 #=> 101
Fred.new.foo #=> 101
Fred.class_vriable_defined? :@@bar #=> false </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">class One
@@var1 = 1
end
class Two < One
@@var2 = 2
end </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">One.class_variables #=> [:@@var1]
Two.class_variables #=> [:@@var2, :@@var1]
Two.class_variables false #=> [:@@var2] </code></pre>
</section>
<section class="center" data-background="#0F0">
<h2 class="white">
Class Variables API
</h2>
</section>
<section>
<h2>
Module#const_defined?
</h2>
<p>
Checks for a constant with the given name in mod
</p>
</section>
<section>
<h2>
Module#const_defined?
</h2>
<p>
If inherit is set, the lookup will also search the ancestors (and Object if mod is a Module)
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">Math.const_defined? "PI" #=> true
IO.const_defined? :SYNC #=> true
IO.const_defined? :SYNC, false #=> false </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">Hash.const_defined? 'foobar' #=> NameError: wrong constant name foobar </code></pre>
</section>
<section data-background="http://media.giphy.com/media/U7l2ANkN9gl9e/giphy.gif"></section>
<section>
<h2>
Module#const_get
</h2>
<p>
Checks for a constant with the given name in mod
</p>
</section>
<section>
<h2>
Module#const_get
</h2>
<p>
If inherit is set, the lookup will also search the ancestors (and Object if mod is a Module)
</p>
</section>
<section>
<h2>
Module#const_get
</h2>
<p>
The value of the constant is returned if a definition is found, otherwise a NameError is raised
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">module Foo
class Bar
end
end
Object.const_get 'Foo::Bar' </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">module Foo
class Bar
VAL = 10
end
class Baz < Bar; end
end
Object.const_get 'Foo::Baz::VAL' # => 10
Object.const_get 'Foo::Baz::VAL', false # => NameError </code></pre>
</section>
<section>
<h2>
Module#const_set
</h2>
<p>
Sets the named constant to the given object, returning that object
</p>
</section>
<section>
<h2>
Module#const_set
</h2>
<p>
Creates a new constant if no constant with the given name previously existed
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0) #=> 3.14285714285714
Math::HIGH_SCHOOL_PI - Math::PI #=> 0.00126448926734968 </code></pre>
</section>
<section>
<h2>
Module#constants
</h2>
<p>
Returns an array of the names of the constants accessible in mod
</p>
</section>
<section>
<h2>
Module#constants
</h2>
<p>
This includes the names of constants in any included modules (example at start of section)
</p>
</section>
<section>
<h2>
Module#constants
</h2>
<p>
Unless the inherit parameter is set to false
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">IO.constants.include?(:SYNC) #=> true
IO.constants(false).include?(:SYNC) #=> false </code></pre>
</section>
<section>
<h2>
Module#const_missing
</h2>
<p>
Invoked when a reference is made to an undefined constant in mod
</p>
</section>
<section>
<h2>
Module#const_missing
</h2>
<p>
It is passed a symbol for the undefined constant
</p>
</section>
<section>
<h2>
Module#const_missing
</h2>
<p>
Returns a value to be used for that constant.
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">def Foo.const_missing(name)
name
end
Foo::UNDEFINED_CONST #=> :UNDEFINED_CONST </code></pre>
</section>
<section class="center" data-background="#0F0">
<h2 class="white">
Methods API
</h2>
</section>
<section>
<h2>
BasicObject#method_missing
</h2>
<p>
Invoked by Ruby when obj is sent a message it cannot handle
</p>
</section>
<section>
<h2>
BasicObject#method_missing
</h2>
<p>
symbol is the symbol for the method called, and args are any arguments that were passed to it
</p>
</section>
<section>
<h2>
BasicObject#method_missing
</h2>
<p>
By default, the interpreter raises an error when this method is called
</p>
</section>
<section>
<h2>
BasicObject#method_missing
</h2>
<p>
However, it is possible to override the method to provide more dynamic behavior
</p>
</section>
<section>
<h2>
BasicObject#method_missing
</h2>
<p>
If it is decided that a particular method should not be handled, then super should be called, so that ancestors can pick up the missing method
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">class ArrayInquirer < Array
def method_missing(name, *args, &block)
if name.end_with? '?'
include? name.to_s.chomp '?'
else
super
end
end
end </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">munchies = ArrayInquirer.new %w(bacon ham eggs cheese)
munchies.bacon? #=> true
munchies.ham? #=> true
muchies.spam? #=> false </code></pre>
</section>
<section>
<h2>
Object#respond_to_missing?
</h2>
<p>
Let us play nicely with Object#respond_to?
</p>
</section>
<section>
<h2>
Object#respond_to_missing?
</h2>
<p>
Hook method to return whether the obj can respond to id method or not
</p>
</section>
<section>
<h2>
Object#respond_to_missing?
</h2>
<p>
When the method name parameter is given as a string, the string is converted to a symbol
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">class ArrayInquirer < Array
def respond_to_missing?(name, include_private = false)
name.end_with? '?'
end
end </code></pre>
</section>
<section>
<pre><code class="ruby" contenteditable="true">munchies = ArrayInquirer.new %w(bacon ham eggs cheese)
muchies.ham? #=> true
muchies.spam? #=> false
munchies.respond_to? :spam? #=> false </code></pre>
</section>
<section>
<h2>
Module#define_method
</h2>
<p>
Defines an instance method in the receiver
</p>
</section>
<section>
<h2>
Module#define_method
</h2>
<p>
The method parameter can be a Proc, a Method or an UnboundMethod object
</p>
</section>
<section>
<h2>
Module#define_method
</h2>
<p>
If a block is specified, it is used as the method body
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">class SweetTalker
define_method :say_something do |something, mood:|
case mood
when :happy then something.capitalize + '!'
when :angry then 'YOU SUCK!'
else 'meh...'
end
end
end </code></pre>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">person = SweetTalker.new
person.say_something 'life is good', mood: :angry #=> "YOU SUCK!"
person.say_something 'life is good', mood: :happy #=> "Life is good!"
person.say_something 'life is good', mood: :whatever #=> "meh..." </code></pre>
</section>
<section>
<h2>
Module#method_added
</h2>
<p>
Invoked as a callback whenever an instance method is added to the receiver
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">module Chatty
def self.method_added(method_name)
puts 'Adding ' + method_name.inspect
end
def self.some_class_method() end
def some_instance_method() end
end
#=> Adding :some_instance_method </code></pre>
</section>
<section>
<h2>
Module#method_removed
</h2>
<p>
Invoked as a callback whenever an instance method is removed from the receiver
</p>
</section>
<section class="center">
<pre><code class="ruby" contenteditable="true">module Chatty
def self.method_removed(method_name)
puts "Removing " + method_name.inspect
end
def some_instance_method() end
remove_method :some_instance_method
end
#=> Removing :some_instance_method </code></pre>
</section>
</div>
</div>
<script src="js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script type="text/javascript">
Reveal.initialize({
width: '100%',
height: '100%',
margin: 0.05,
controls: false,
progress: false,
history: true,
center: false,
rollingLinks: true,
transition: 'none',
backgroundTransition: 'none',
dependencies: [
{
src: 'js/highlight.js',
async: true,
callback: function() {
hljs.configure({ languages: ['ruby'] });
hljs.initHighlightingOnLoad();
}
}
]
});
</script>
</body>
</html>