-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path10things.html
1285 lines (951 loc) · 31.2 KB
/
10things.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
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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>10 Things Every Java Programmer Should Know About Ruby</title>
<meta name="generator" content="Slide Show (S9) 2.0.1 on Ruby 1.9.2 (2011-07-09) [i386-mingw32]">
<meta name="author" content="Jim Weirich" >
<!-- helper/macro that lets you add (CSS3) gradient using headers
see http://slideshow.rubyforge.org/themes.html
-->
<!-- S6 style sheet links -->
<link rel="stylesheet" href="10things.css" media="projection" id="styleProjection">
<link rel="stylesheet" href="s6/screen.css" media="screen" id="styleScreen">
<link rel="stylesheet" href="s6/print.css" media="print">
<!-- S6 JS -->
<script src="s6/jquery.js"></script>
<script src="s6/jquery.slideshow.js"></script>
<script>
$(document).ready( function() {
Slideshow.init();
} );
</script>
<!-- Better Browser Banner for Microsoft Internet Explorer (IE) -->
<!--[if IE]>
<script src="s6/jquery.microsoft.js"></script>
<![endif]-->
</head>
<body>
<div class="layout">
<div id="header"></div>
<div id="footer">
<h1>Your Footer Here</h1>
<h2>Your Subfooter Here</h2>
</div>
</div>
<div class="presentation">
<div class='slide '>
<!-- === begin markdown block =====================================================
generated by markdown 1.0.0 on Ruby 1.9.2 (2011-07-09) [i386-mingw32]
on 2013-06-21 19:57:13 +0200 with Markdown engine kramdown (1.0.2)
using options { !to be done! }
-->
<!-- _S9SLIDE_ -->
<h1 id="things-every-java-programmer-should-know-about-ruby">10 Things Every Java Programmer Should Know About Ruby</h1>
<p class="center">Jim Weirich</p>
<p class="center">Adapted S6/S9 Version <sup>1</sup> from <a href="http://onestepback.org/articles/10things">Original Slide Deck</a></p>
<!-- begin help -->
<div class="help projection">
<p><strong>Slide Show Keyboard Controls (Help)</strong></p>
<table>
<tr>
<td> Action </td>
<td> Key </td>
</tr>
<tr>
<td> Go to next slide </td>
<td> Space Bar, Right Arrow, Down Arrow, Page Down, Click Heading </td>
</tr>
<tr>
<td> Go to previous slide </td>
<td> Left Arrow, Up Arrow, Page Up </td>
</tr>
<tr>
<td> Toggle between slideshow and outline view (Ø) </td>
<td> T </td>
</tr>
<tr>
<td> Show/hide slide controls (Ø « ») </td>
<td> C, Move mouse to bottom right corner </td>
</tr>
<tr>
<td> Zoom in, zoom out, zoom reset (100%) </td>
<td> Control<code>+</code>Plus, Control<code>+</code>Minus, Control<code>+</code><code>0</code> </td>
</tr>
</table>
</div>
<!-- end help -->
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="who-am-i">Who Am I?</h1>
<ul>
<li>Jim Weirich</li>
<li>Consultant for Compuware</li>
<li>Java Programmer</li>
<li>Ruby Enthusiast</li>
</ul>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="first-things-first">First Things First</h1>
<p>I used to teach an after hours course in C programming for employees of a large manufacturing company.
It was pretty easy to tell what programming languages the students had used previously just
by looking at the style of C code they produced. It is certainly true that
“You can write FORTRAN in any language”.</p>
<p>Java programmers investigating Ruby will find a language that look similar in many ways.
There are classes and modules, namespaces and scopes, instance variables and methods.
A Java programmer will feel quite at home in this Object Oriented language.</p>
<p>So the temptation will be to continue to program in a Java-style.
Sure, there are some things that are different (the lack of type declarations will probably
be the first thing that strikes them).
But nothing that can’t be worked around with a little effort … and they will miss a golden opportunity.</p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="sapir-whorf-hypothesis">Sapir-Whorf Hypothesis</h1>
<p>The Sapir-Whorf Hypothesis theorizes that thoughts and behavior are determined
(or are at least partially influenced) by language. […] To this day it has not been completely
disputed or defended, but has continued to intrigue researchers around the world.</p>
<h3 id="quote">Quote</h3>
<blockquote>
<p>A language that doesn’t affect the way you think about programming
is not worth knowing – Alan Perlis </p>
</blockquote>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="the-ruby-way">The Ruby Way</h1>
<p>This is <em>not</em> yet another “Ruby is better than Java” article.
Nor is it intended to bash Java or Java programmers.
Rather, it is an attempt to aid Java programmers who are investigating Ruby
by helping them quickly get over the “<em>Writing Java in Ruby</em>” syndrome and to discover the Ruby Way.</p>
<p>Now, on to our 10 Things…</p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="item-10---learn-ruby-conventions">Item #10 - Learn Ruby Conventions</h1>
<ul>
<li><code>ClassNames</code></li>
<li><code>method_names</code> and <code>variable_names</code></li>
<li><code>methods_asking_a_question?</code></li>
<li><code>slightly_dangerous_methods!</code></li>
<li><code>@instance_variables</code></li>
<li><code>$global_variables</code></li>
<li><code>SOME_CONSTANTS</code> or <code>OtherConstants</code></li>
</ul>
<p>Some of the conventions are enforced by the language,
others are merely standards used by the community.</p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="item-9---everything-is-an-object">Item #9 - Everything is an Object</h1>
<p>Everything in Ruby that can be bound to a variable name
is a full-fledged object.</p>
<p>This has interesting consequences</p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="classes-are-objects">Classes are Objects!</h1>
<ul>
<li><code>Array</code> is a constant name that is bound to the Array class object.</li>
<li>
<p>Creating new objects does not require special syntax. We just send <code>new</code> to the class object.
{: .clear}<!-- workaround: note - list includes code block with block attribute --></p>
<p>a = Array.new</p>
</li>
</ul>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="this-make-factories-trivial">This make factories trivial</h1>
<p>Since Classes create instances of themselves,
they are the ultimate factory object.</p>
<pre><code>def create_from_factory(factory)
factory.new
end
obj = create_from_factory(Array)
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="no-primitives">No Primitives!</h1>
<p>Even integers are full fledged objects.</p>
<pre><code>0.zero? # => true
1.zero? # => false
1.abs # => 1
-1.abs # => 1
1.methods # => list of methods for object 1
2.+(3) # => 5 (same as 2+3)
10.class # => Fixnum
(10**100).class
# => Bignum
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="nil-is-an-object"><code>nil</code> is an Object!</h1>
<ul class="clear">
<li>Java:
<ul>
<li><code>null</code> means “no reference to object”</li>
<li>Invoking a method on <code>null</code> is an error</li>
</ul>
</li>
<li>Ruby:
<ul>
<li><code>nil</code> is a normal object</li>
<li>You can never get a null pointer error!</li>
</ul>
</li>
</ul>
<pre><code>a = nil
a.nil? # => true
a.methods # => list of methods
a.abs # => NoMethodError
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="things-that-are-not-objects">Things that are not Objects</h1>
<ul>
<li>Variable names are not objects
<ul>
<li>You cannot have a variable reference another variable
<ul>
<li>(no indirection to variables)</li>
</ul>
</li>
<li>There are workarounds to this
<ul>
<li>But that’s an advanced topic</li>
<li>And no one really <em>needs</em> it anyways.</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="more-things-that-are-not-objects">More Things that are not Objects</h1>
<ul class="clear">
<li>Blocks are not objects
<ul>
<li>But that’s a distinction without a difference</li>
<li>By the time you need them, they automatically convert to <code>Proc</code> objects</li>
</ul>
</li>
</ul>
<!-- begin left {:class=>"code"} -->
<div class="code">
<table width="100%"><tr><td width="50%" style="vertical-align: top;">
<pre><code>def with_block
yield
end
with_block {
# Never converted
}
</code></pre>
</td>
<!-- end left -->
<!-- begin right {:class=>"code"} -->
<td width="50%" style="vertical-align: top;">
<pre><code>def with_proc(&block)
block.call
end
with_proc {
# Converted internally
}
</code></pre>
</td></tr></table>
</div>
<!-- end right -->
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="item-8---almost-everything-is-a-message">Item #8 - (Almost) Everything is a Message</h1>
<p>All computation in Ruby happens through:</p>
<ul>
<li>Binding names to objects (assignment)</li>
<li>Primitive controls structures (e.g. <code>if</code>/<code>else</code>, <code>while</code>) and operators (e.g. <code>defined?</code>)</li>
<li>Sending messages to objects</li>
</ul>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="yes-all-of-these-are-messages">Yes, all of these are messages…</h1>
<dl>
<dt><code>string.index("x")</code></dt>
<dd>Send <code>:index</code> (with argument “<code>x</code>”)</dd>
<dt><code>string.length</code></dt>
<dd>Send <code>:length</code> (with no argument)</dd>
<dt><code>run_status_reports</code></dt>
<dd>Send <code>:run_status_reports</code> (to <code>self</code>)</dd>
<dt><code>1 + 2</code></dt>
<dd>Send <code>:+</code> (with argument <code>2</code>) to the object <code>1</code></dd>
<dt><code>array[i]</code></dt>
<dd>Send <code>:[]</code> (with argument <code>i</code>) to the array</dd>
</dl>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="messages-not-function-calls">Messages, not Function Calls</h1>
<p>Java Programmers tend to think of <code>obj.method()</code> as looking up
a member function in a table and calling it.</p>
<p>Ruby programmers tend to think of <code>obj.method</code> as sending a message
to an object.</p>
<p class="center">What’s the Difference?</p>
<p>The difference is subtle, but important!</p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="what-kind-of-differences">What <em>Kind</em> of Differences?</h1>
<p>Consider the following class.
It defines an object that is able to record all the messages ever sent to it, and then playback those messages to another object.</p>
<pre><code>class VCR
def initialize
@messages = []
end
def method_missing(method, *args, &block)
@messages << [method, args, block]
end
def play_back_to(obj)
@messages.each do |method, args, block|
obj.send(method, *args, &block)
end
end
end
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="playing-back-data">Playing Back Data</h1>
<p>Example Code</p>
<pre><code>require 'src/vcr'
vcr = VCR.new
vcr.sub!(/Java/) { "Ruby" }
vcr.upcase!
vcr[11,5] = "Universe"
vcr << "!"
string = "Hello Java World"
puts string
vcr.play_back_to(string)
puts string
</code></pre>
<p>Output</p>
<pre><code>Hello Java World
HELLO RUBY Universe!
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="message-opportunities">Message Opportunities…</h1>
<dl>
<dt><strong>Remote Proxies</strong></dt>
<dd>Automatically forward any message to a remote object.</dd>
<dt><strong>Auto Loaders</strong></dt>
<dd>Stand in for an object until it gets its first message. Then load it and act like a regular proxy. Great for autoloading database backed objects.</dd>
<dt><strong>Decorators</strong></dt>
<dd>Intercept the messages you want and pass the rest through.</dd>
<dt><strong>Mock Objects</strong></dt>
<dd>Just write the methods that need to be mocked. Proxy or ignore the others as needed.</dd>
<dt><strong>Builders</strong></dt>
<dd>Generate XML/HTML/Whatever based on the methods called on the builder</dd>
</dl>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="item-7---ruby-is-way-more-dynamic-than-you-expect">Item #7 - Ruby is <em>Way</em> More Dynamic Than You Expect</h1>
<p>One of the big attractions of Java over C++ was the dynamic features
of the language. You could easily load classes at run time,
query objects about their classes and methods,
and even call methods discovered at runtime. </p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="dynamic-beyond-java">Dynamic Beyond Java</h1>
<p>Ruby takes dynamic behavior several steps beyond Java.</p>
<!-- begin left {} -->
<table width="100%"><tr><td width="50%" style="vertical-align: top;">
<ul>
<li><code>method_missing</code></li>
<li>Easy Reflection</li>
<li>Open Classes</li>
</ul>
</td>
<!-- end left -->
<!-- begin right {} -->
<td width="50%" style="vertical-align: top;">
<ul>
<li>Singleton Objects</li>
<li>Definition Hooks</li>
<li>Code Evalutation</li>
</ul>
</td></tr></table>
<!-- end right -->
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="easy-reflection-create-object">Easy Reflection: Create Object</h1>
<pre><code>public static Object create(Class c, String value)
throws Exception
{
Constructor ctor = c.getConstructor(
new Class[] { String.class } );
return ctor.newInstance( new Object[] { "Hello" } );
}
public static void main (String args[])
throws Exception
{
Greeting g =(Greeting) create(Greeting.class, "Hello");
g.show();
}
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="easy-reflection">Easy Reflection</h1>
<p>The Ruby Version</p>
<pre><code>def create(klass, value)
klass.new(value)
end
g = create(Greeting, "Hello")
g.show
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="open-classes">Open Classes</h1>
<p>Methods can be added to classes at any point … even built in classes. </p>
<pre><code>class Integer
def even?
(self % 2) == 0
end
end
p (1..10).select { |n| n.even? }
# => [2, 4, 6, 8, 10]
</code></pre>
<p>Caution is advised, but this feature can be very useful.</p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="singleton-methods">Singleton Methods</h1>
<p>Singleton methods are defined on individual objects, not classes.</p>
<pre><code>class Dog
end
rover = Dog.new
fido = Dog.new
def rover.speak
puts "Red Rover"
end
rover.speak # => "Red Rover"
fido.speak # => NoMethodError
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="hooks">Hooks</h1>
<p>Hooks allow the user to gain control at interesting moments
during the execution of a program.</p>
<pre><code>class MyClass
def MyClass.method_added(name)
puts "Adding Method #{name}"
end
def new_method
# Yada yada yada
end
end
</code></pre>
<p>Output</p>
<pre><code>Adding Method new_method
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="code-eval">Code Eval</h1>
<pre><code>class Module
def trace_attr(sym)
self.module_eval %{
def #{sym}
printf "Accessing %s with value %s\n",
"#{sym}", @#{sym}.inspect
@#{sym}
end
}
end
end
class Dog
trace_attr :name
def initialize(string)
@name = string
end
end
Dog.new("Fido").name # => Accessing name with value "Fido"
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="item-6---objects-are-strongly-typed---not-statically-typed">Item #6 - Objects are Strongly Typed - Not Statically Typed</h1>
<p class="center"><em>What is a Type?</em></p>
<p class="center">A type is<br />
<strong>a set of values</strong><br />
and<br />
<strong>a set of operations</strong> </p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<!-- begin left {} -->
<table width="100%"><tr><td width="50%" style="vertical-align: top;">
<h3 id="c-code-weak">C Code (Weak)</h3>
<pre><code>#include <stdio.h>
extern float two();
int main() {
float x = 1.5 + two();
printf("%f\n", x);
printf("%d\n", x);
return 0;
}
</code></pre>
</td>
<!-- end left -->
<!-- begin right {} -->
<td width="50%" style="vertical-align: top;">
<h3 id="java-code-strong">Java Code (Strong)</h3>
<pre><code>public class Main {
public static
void main (String args[]) {
double x = 1.5 + Two.two();
System.out.println(x);
}
}
</code></pre>
</td></tr></table>
<!-- end right -->
<!-- begin left {} -->
<table width="100%"><tr><td width="50%" style="vertical-align: top;">
<pre><code>int two() { return 2; }
</code></pre>
</td>
<!-- end left -->
<!-- begin right {} -->
<td width="50%" style="vertical-align: top;">
<pre><code>public class Two {
public static int two() {
return 2;
}
}
</code></pre>
</td></tr></table>
<!-- end right -->
<!-- begin left {} -->
<table width="100%"><tr><td width="50%" style="vertical-align: top;">
<h3 id="output">Output</h3>
<pre><code>nan
0
</code></pre>
</td>
<!-- end left -->
<!-- begin right {} -->
<td width="50%" style="vertical-align: top;">
<h3 id="output-1">Output</h3>
<pre><code>3.5
</code></pre>
</td></tr></table>
<!-- end right -->
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<!-- begin left {} -->
<table width="100%"><tr><td width="50%" style="vertical-align: top;">
<h3 id="ruby-code-">Ruby Code (?)</h3>
<pre class="clear"><code>require 'two'
x = 1.5 + two
puts x
printf "%d", x
</code></pre>
<pre><code>def two
2
end
</code></pre>
<h3 id="output-2">Output</h3>
<pre><code>3.5
3
</code></pre>
</td>
<!-- end left -->
<!-- begin right {} -->
<td width="50%" style="vertical-align: top;">
<h3 id="so-what-makes-a-language-type-safe">So what makes a language type safe?</h3>
<ul>
<li>Compiler knowledge of the variable types?</li>
<li>Declaring all variables?</li>
<li>Compiler catching all type errors?</li>
</ul>
<p>Or…</p>
<!-- begin step {} -->
<div class="step">
<ul>
<li>Catching all inappropriate operations on a type, either at
<ul>
<li>compile time, or</li>
<li>run time</li>
</ul>
</li>
</ul>
</div>
<!-- end step -->
</td></tr></table>
<!-- end right -->
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<!-- begin left {} -->
<table width="100%"><tr><td width="50%" style="vertical-align: top;">
<h3 id="ruby-code">Ruby Code</h3>
<pre><code>def factorial(n)
result = 1
(2..n).each do |i|
result *= i
end
result
end
puts factorial(20)
puts factorial(21)
</code></pre>
</td>
<!-- end left -->
<!-- begin right {} -->
<td width="50%" style="vertical-align: top;">
<h3 id="java-code">Java Code</h3>
<pre><code>public class Fact {
static long factorial(long n) {
long result = 1;
for (long i=2; i<=n; i++)
result *= i;
return result;
}
public static
void main (String args[]) {
System.out.println(factorial(20));
System.out.println(factorial(21));
}
}
</code></pre>
</td></tr></table>
<!-- end right -->
<!-- begin left {} -->
<table width="100%"><tr><td width="50%" style="vertical-align: top;">
<h3 id="output-3">Output</h3>
<pre><code>2432902008176640000
51090942171709440000
</code></pre>
</td>
<!-- end left -->
<!-- begin right {} -->
<td width="50%" style="vertical-align: top;">
<h3 id="output-4">Output</h3>
<pre><code>2432902008176640000
-4249290049419214848
</code></pre>
</td></tr></table>
<!-- end right -->
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="lanaguage-typing-systems">Lanaguage Typing Systems</h1>
<!-- begin left {} -->
<table width="100%"><tr><td width="50%" style="vertical-align: top;">
<h3 id="java-is">Java is</h3>
<ul>
<li>Strongly,</li>
<li>Statically,</li>
<li>Manifestly</li>
</ul>
<p>typed.</p>
</td>
<!-- end left -->
<!-- begin right {} -->
<td width="50%" style="vertical-align: top;">
<h3 id="ruby-is">Ruby is</h3>
<ul>
<li>Strongly,</li>
<li>Dynamically,</li>
<li>Implicitly</li>
</ul>
<p>typed.</p>
</td></tr></table>
<!-- end right -->
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="testimonial">Testimonial</h1>
<blockquote>
<p>I’ve been a statically typed bigot for quite a few years.
I learned my lesson the hard way while using C.
Too many systems crashed in the field due to silly typing errors. […]</p>
</blockquote>
<blockquote>
<p>Four years ago I got involved with Extreme Programming. […] I can’t
imagine not having a comprehensive suite of unit tests to back up
my development. […]</p>
</blockquote>
<blockquote>
<p>About two years ago I noticed something.
I was depending less and less on the type system for safety.
My unit tests were preventing me from making type errors. […]</p>
</blockquote>
<blockquote>
<p>So I tried writing some applications in Python, and then Ruby.
I was not entirely surprised when I found that type issues simply
never arose.</p>
</blockquote>
<p>– <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=4639">Bob Martin</a></p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="item-5---dont-worry-about-interfaces">Item #5 - Don’t Worry About Interfaces</h1>
<!-- begin left {} -->
<table width="100%"><tr><td width="50%" style="vertical-align: top;">
<h3 id="ruby-uses-duck-typing">Ruby Uses Duck Typing</h3>
<ul>
<li>If it walks like a duck,</li>
<li>And talks like a duck,
<ul>
<li>Then we can treat it like a duck.</li>
<li>(who cares what it <em>really</em> is)</li>
</ul>
</li>
</ul>
</td>
<!-- end left -->
<!-- begin right {} -->
<td width="50%" style="vertical-align: top;">
<pre><code>class Duck
def talk() puts "Quack" end
end
class DuckLikeObject
def talk() puts "Kwak" end
end