-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
3361 lines (2762 loc) · 157 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
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>
<!-- Microdata markup added by Google Structured Data Markup Helper. -->
<html lang="en" style="overflow-x:hidden;">
<head>
<title>devCHEATs</title>
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<meta content="devCHEATs - Programming Reference - Ruby, JavaScript, HTML, CSS, SQL"name="description">
<meta content="Joe Wilmoth" name="author">
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/bootstrap-responsive.min.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!-- [if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif] -->
<link href="css/style.css" rel="stylesheet">
<link href="css/pricing.css" rel="stylesheet">
<link href="css/m-styles.min.css" rel="stylesheet">
<link href="http://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700" rel="stylesheet" type="text/css">
<link href="thumbnail.png" rel="image_src">
<link href="favicon.ico" rel="icon" type="image/ico">
</head>
<body data-offset="64" data-spy="scroll" data-target=".navbar" onload="$.stellar();" style="">
<div id="preloader" style="display: none;">
<div id="status" style="display: none;"></div>
</div><!-- start navigation -->
<div class="navbar navbar-fixed-top" id="navigation">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-target=".nav-collapse"
data-toggle="collapse"></a>
<h3><a class="brand" href="#intro">devCHEATs.com</a></h3>
<a class="brand" href="#intro"></a>
<div class="nav-collapse collapse">
<ul class="nav pull-right">
<li class="active"><a href="#intro">Top</a></li>
<li><a href="#services-top"><span>Ruby</span></a></li>
<li><a href="#works-top"><span>HTML +</span>
<span>CSS</span></a></li>
<li><a href=
"#gallery-top"><span>Javascript</span></a></li>
<li><a href="#team-top"><span>SQL</span></a></li>
<li><a href=
"#contact-top"><span>Contact</span></a></li>
</ul>
</div>
</div>
</div>
</div>
<section id="intro">
<div class="container">
<div class="row">
<div class="span8 offset2 margin25">
<div class="carousel slide carousel-fade" id=
"carousel_fade_intro">
<div class="carousel-inner">
<div class="active item">
<h1> CheatSheet for </h1>
</div>
<div class="inactive item">
<h1> Ruby + JavaScript + HTML<br> <h2>CSS + SQL</h2> </h1>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="fadeInUp delay animated hidden-phone" id="more">
<a class="m-btn a-btn blue big icn-only" href="#services-top"></a>
</div>
</section><!--start services header-->
<section id="services-top">
<!--start services-desktop header-->
<section class="visible-desktop" data-stellar-background-ratio="0.6"
data-stellar-vertical-offset="20" id="services-top-desktop">
<h1 class="header">Ruby</h1>
<p class="header">Basic Ruby Stuff</p>
</section><!--start services-mobile header-->
<section class="hidden-desktop" id="services-top-mobile">
<h1 class="header">Ruby</h1>
<p class="header">Basic Ruby Stuff</p>
</section>
</section><!--start services-->
<section id="services">
<div class="container">
<div class="row divide">
<div class="span12">
<ul class="thumbnails">
<li class="span3">
<!-- STANDARD PRICE ITEM -->
<div class="pricing-table red">
<!-- BODY BOX-->
<div class="pricing-table-header">
<!-- HEADER BOX-->
<span class="heading">Ruby</span>
<span class=
"price-value">general<span class=
"mo">concepts</span></span>
</div><!--/ BODY BOX-->
<div class="pricing-table-space"></div>
<!-- CONTENT BOX-->
<div class="pricing-table-features">
<p><strong>Variables:</strong> variable =
some_value</p>
<p><strong>Console Output:</strong> puts
something</p>
<p><strong>Call method:</strong>
object.method(arguments)</p>
<p><strong>Equality:</strong> object ==
other</p>
<p><strong>Inequality:</strong> object !=
other</p>
<p><strong>if</strong>
<em>condition</em><br>
# happens when true<br>
<strong>else</strong><br>
# happens when false<br>
<strong>end</strong></p>
</div><!-- / CONTENT BOX-->
</div><!--/ BODY BOX-->
<!-- STANDARD PRICE ITEM -->
</li>
<li class="span3">
<!-- STANDARD PRICE ITEM -->
<div class="pricing-table red">
<!-- BODY BOX-->
<div class="pricing-table-header">
<!-- HEADER BOX-->
<span class="heading">Ruby</span>
<span class="price-value">NUM<span class=
"mo">bers</span></span>
</div><!--/ BODY BOX-->
<div class="pricing-table-space"></div>
<!-- CONTENT BOX-->
<div class="pricing-table-features">
<p><strong>Number:</strong> 0, 1, -42</p>
<p><strong>Decimals:</strong> 5.1, 8.7,
-3.0</p>
<p><strong>Basic Math:</strong> a operator
b</p>
<p><strong>Comparison:</strong> a operator
b</p>
<p><strong>Math operators:</strong> +, -,
*, /, %</p>
<p><strong>Comparison operators:</strong>
>, <, >=</p>
</div><!-- / CONTENT BOX-->
</div><!--/ BODY BOX-->
<!-- STANDARD PRICE ITEM -->
</li>
<li class="span3">
<!-- STANDARD PRICE ITEM -->
<div class="pricing-table red">
<!-- BODY BOX-->
<div class="pricing-table-header">
<!-- HEADER BOX-->
<span class="heading">Ruby</span>
<span class="price-value">STR<span class=
"mo">ings</span></span>
</div><!--/ BODY BOX-->
<div class="pricing-table-space"></div>
<!-- CONTENT BOX-->
<div class="pricing-table-features">
<p><strong>Create:</strong> 'A string'</p>
<p><strong>Interpolation:</strong><br>
"A string and #{expression}!"</p>
<p><strong>Length:</strong>
string.length</p>
<p><strong>Concatenate:</strong> string +
string2</p>
<p><strong>Substitute:</strong><br>
string.gsub(a_string, substitute)</p>
<p><strong>Access:</strong>
string[position]</p>
</div><!-- / CONTENT BOX-->
</div><!--/ BODY BOX-->
<!-- STANDARD PRICE ITEM -->
</li>
<li class="span3">
<!-- STANDARD PRICE ITEM -->
<div class="pricing-table red">
<!-- BODY BOX-->
<div class="pricing-table-header">
<!-- HEADER BOX-->
<span class="heading">Ruby</span>
<span class="price-value">AR<span class=
"mo">rays</span></span>
</div><!--/ BODY BOX-->
<div class="pricing-table-space"></div>
<!-- CONTENT BOX-->
<div class="pricing-table-features">
<p><strong>Create:</strong> [contents]</p>
<p><strong>Number of elements:</strong>
array.size</p>
<p><strong>Access:</strong>
array[position]</p>
<p><strong>Adding an element:</strong><br>
array << element</p>
<p><strong>Assigning:</strong><br>
array[number] = value</p>
<p><strong>Delete at index:</strong><br>
array.delete_at(i)</p>
<p><strong>Iterating:</strong><br>
array.each <em>do</em> |x| ..
<em>end</em></p>
</div><!-- / CONTENT BOX-->
</div><!--/ BODY BOX-->
</li><!-- STANDARD PRICE ITEM -->
</ul>
</div>
</div>
<div class="span8 offset1" id="lesson_content">
<h2>Variable Vocabulary</h2>
<p>Here's some vocabulary related to variables. Look at the
following code:</p>
<div class="highlight">
<pre>
<span class="n">first_name</span> <span class="o">=</span> <span class=
"s2">"Jesse"</span>
<span class="n">last_name</span> <span class="o">=</span> <span class=
"s2">"Farmer"</span>
<span class="n">full_name</span> <span class="o">=</span> <span class=
"s2">"</span><span class="si">#{</span><span class=
"n">first_name</span><span class="si">}</span><span class=
"s2"> </span><span class="si">#{</span><span class=
"n">last_name</span><span class="si">}</span><span class="s2">"</span>
<span class="n">num1</span> <span class="o">=</span> <span class="mi">10</span>
<span class="n">num2</span> <span class="o">=</span> <span class="mi">45</span>
<span class="n">sum</span> <span class="o">=</span> <span class=
"n">num1</span> <span class="o">+</span> <span class="n">num2</span>
<span class="n">sum</span> <span class="o">+=</span> <span class="mi">5</span>
</pre>
</div>
<p>The following sentences are all true.</p>
<ul>
<li>The single equals character <code>=</code> is called
the <span class="keyword">assignment operator</span>.</li>
<li>The variable <code>first_name</code> is assigned the
value <code>"Jesse"</code>, a <code>String</code>.</li>
<li>The variable <code>last_name</code> contains the string
<code>"Farmer"</code></li>
<li><code>full_name</code> is a variable created by
interpolating the values of <code>first_name</code> and
<code>last_name</code>, separated by a space</li>
<li><code>num1</code> is equal to <code>10</code>, an
<code>Integer</code></li>
<li><code>sum</code> is equal to <code>55</code>, which is
the sum of the values of <code>num1</code> and
<code>num2</code></li>
<li>The last line increments <code>sum</code> by
<code>5</code>, so <code>sum</code> is now
<code>60</code></li>
</ul>
<h3 id="toc_0">Rules of Variable Naming</h3>
<p>In Ruby, variables must begin with a lower-case letter
(<code>a</code> through <code>z</code>) or an underscore
(<code>_</code>). After the first character, they can also
contain upper-case letters and numbers.</p>
<p>That is,</p>
<div class="highlight">
<pre>
<span class="n">num</span>
<span class="n">_num</span>
<span class="n">nUM</span>
<span class="n">num_123</span>
</pre>
</div>
<p>are all valid variable names. These, however, are invalid
and will result in an error if you try to use them as
variables:</p>
<div class="highlight">
<pre>
<span class="n">Pants</span>
1<span class="n">dog</span>
<span class="n">num</span><span class="c">%^&</span>
</pre>
</div>
<p>By convention, variables never contain upper-case letters.
Breaks in words (periods, spaces, etc.) are replaced by
underscores. These are all good variable names:</p>
<div class="highlight">
<pre>
<span class="n">seconds_so_far</span>
<span class="n">first_name</span>
<span class="n">last_name</span>
<span class="n">created_at</span>
<span class="n">last_updated_at</span>
</pre>
</div>
</div>
<div class="span8 offset1" id="lesson_content">
<h2>Writing Methods</h2>
<p>In addition to the built in methods that Ruby provides, we
can also write our own methods. Whenever we have an action we
want to perform like adding two numbers or parsing a string, we
will define that action in a method.</p>
<p>The general syntax for defining a method is:</p>
<div class="highlight">
<pre>
<span class="k">def</span> <span class="nf">method_name</span><span class=
"p">(</span><span class="n">parameter</span> <span class=
"n">list</span><span class="p">)</span>
<span class="c1"># ... one or more statements ...</span>
<span class="k">return</span> <span class="n">value</span>
<span class="k">end</span>
</pre>
</div>
<p>The keywords <code>def</code> and <code>end</code> signify
the beginning and the end of the method. The
<code>method_name</code> should be meaningful and represent
what the statements in the method do. The <code>(parameter
list)</code> is the values that are sent to the method when it
is called.</p>
<p>If there are no parameters then the parentheses can be empty
or not added at all. Inside the method there are statements
which perform the action of the method and a
<code>return</code> statement that sends a value back to the
place where the method was called.</p>
<p>For example an <code>add</code> method would look like:</p>
<div class="highlight">
<pre>
<span class="k">def</span> <span class="nf">add</span><span class=
"p">(</span><span class="n">num1</span><span class="p">,</span> <span class=
"n">num2</span><span class="p">)</span>
<span class="k">return</span> <span class="n">num1</span> <span class=
"o">+</span> <span class="n">num2</span>
<span class="k">end</span>
</pre>
</div>
<p>When we define a method, we will either write it in a
<code>Class</code> or a <code>Module</code>. If it is in a
<code>Module</code>, then it can be called without explicitly
defining an instance object to call it on by just using the
name and the arguments, e.g., <code>add(3,4)</code>.</p>
<p>Type the add method into the <code>SANDBOX</code> and then
practice calling it with several different arguments. Don't
forget to add a <code>puts</code> in front of each call so you
can see the output.</p>
<p>Methods can also work with strings, contain calls to other
methods and lots more. Consider the <code>greeting</code>
method</p>
<div class="highlight">
<pre>
<span class="k">def</span> <span class="nf">greeting</span><span class=
"p">(</span><span class="n">firstname</span><span class=
"p">,</span> <span class="n">lastname</span><span class="p">)</span>
<span class="n">fullname</span> <span class="o">=</span> <span class=
"n">firstname</span><span class="o">.</span><span class=
"n">downcase</span><span class="o">.</span><span class=
"n">capitalize</span> <span class="o">+</span> <span class=
"s2">" "</span> <span class="o">+</span> <span class=
"n">lastname</span><span class="o">.</span><span class=
"n">downcase</span><span class="o">.</span><span class="n">capitalize</span>
<span class="k">return</span> <span class="s2">"Hello "</span> <span class=
"o">+</span> <span class="n">fullname</span> <span class=
"o">+</span> <span class="s2">", would you like to learn to program?"</span>
<span class="k">end</span>
</pre>
</div>
<p>What happens when you call this method with different
input?</p>
<ul>
<li><code>greeting("Barack", "obama")</code></li>
<li><code>greeting("QUEEN", "Elizabeth")</code></li>
</ul>
</div>
<div class="span8 offset1" id="lesson_content">
<h2>Array Basics</h2>
<p>Arrays are the most fundamental kind of collection. Think of
them as an ordered list, like the months in the year or a list
of today's chores. The bits and bytes on your hard drive and in
your computer's memory can also be thought of as an array, laid
out physically.</p>
<p>Every language has at least one way of representing ordered
lists. The terminology might differ slightly — arrays, lists,
and vectors are all common terms — but the core concept is the
same. In Ruby we use the <code>Array</code> class to represent
ordered lists.</p>
<div class="definition">
<p>The individual items of an array are called <span class=
"keyword">elements</span> or <span class=
"keyword">members</span>.</p>
</div>
<h3 id="toc_0">Why arrays?</h3>
<p>Why do we need arrays? Imagine a Ruby program that asks you
to enter any number of sentences, one line at a time. When you
type <code>done</code>, it prints out the sentences from
shortest to longest, regardless of what order you entered
them.</p>
<p>How could we write a program that does that? We don't know
beforehand how many sentences the user will enter or how long
they will be. What if the user decided to paste in the complete
works of Shakespeare one line at a time? That's a lot of long,
long lines.</p>
<p>Without an <code>Array</code> we'd have to resort to
something crazy like using a separate variable for each line,
but of course we don't know how many variables we'd need as
we're writing the program. With an <code>Array</code>, we just
read in each line and append them successively to the end of
the <code>Array</code>.</p>
<h3 id="toc_1">Array Questions</h3>
<p>Arrays are great at answering questions like "What's the
first thing on the list?", "What's the last thing on the
list?", "What's the fourth thing on the list?", "How long is
the list?", etc.</p>
<p>Arrays can also be empty, like a blank sheet of paper. "Are
you empty?" is also a question an <code>Array</code> would be
happy to answer.</p>
<h3 id="toc_2">Creating Arrays</h3>
<p>Creating a new <code>Array</code> is easy. Type the
following into the <code>SANDBOX</code>:</p>
<div class="highlight">
<pre>
<span class="n">powers_of_ten</span> <span class="o">=</span> <span class=
"o">[</span><span class="mi">1</span><span class="p">,</span><span class=
"mi">10</span><span class="p">,</span><span class="mi">100</span><span class=
"o">]</span>
<span class="nb">puts</span> <span class="n">powers_of_ten</span><span class=
"o">.</span><span class="n">inspect</span>
</pre>
</div>
<p>The variable <code>powers_of_ten</code> now holds the
<code>Array</code> <code>[1,10,100]</code> as its value. The
first element is <code>1</code>, the second is <code>10</code>,
and the third is <code>100</code>.</p>
<h3 id="toc_3">puts and arrays</h3>
<p>Why did we run <code>puts powers_of_ten.inspect</code>
instead of <code>puts powers_of_ten</code>? Try it:</p>
<div class="highlight">
<pre>
<span class="n">powers_of_ten</span> <span class="o">=</span> <span class=
"o">[</span><span class="mi">1</span><span class="p">,</span><span class=
"mi">10</span><span class="p">,</span><span class="mi">100</span><span class=
"o">]</span>
<span class="nb">puts</span> <span class="n">powers_of_ten</span>
</pre>
</div>
<p>You can see that rather than printing out the array, Ruby
prints outs the contents of the array, one line at a time. This
special behavior is hard-coded into the lowest levels of Ruby.
It's inconsistent with the rest of the language, but remains
there to be backwards-compatible with the oldest versions of
the Ruby language.</p>
<p>Calling <code>Array#inspect</code> before printing the array
will cause <code>puts</code> to print the actual array.</p>
<h3 id="toc_4">A Metaphor</h3>
<p>If you're having trouble thinking about collections, imagine
them as a storage room full of boxes and manned by a concierge.
You don't have direct access to the storage room, but you can
ask the concierge for information about what's in the storage
room, tell him to put something new into the storage room, or
tell him to take something out of the storage room.</p>
<p>To do that, though, you need to have some convention for
referring to particular boxes. An <code>Array</code> is like a
storage room where the boxes are ordered from left to right.
Rather than asking for a box with a particular label, you ask
about the first box, the second box, etc.</p>
<p>And of course you can place a new box at the beginning or
the end of the ordered collection of boxes.</p>
<h3 id="toc_5">Thought Experiment</h3>
<p>You're the concierge. You take out your sharpie and write
the box number on each box, from '#1' to '#10' if there are 10
boxes. You'll need to keep these numbers up-to-date if someone
ever adds or removes something from the storage unit.</p>
<p>Which request would be more annoying to fulfill? Someone
asking you to put a new box after the last box, or put a new
box before the first box?</p>
<p>The exact same reasoning applies to computer arrays.</p>
<h3 id="toc_6">Creating More Arrays</h3>
<p>Type the following, one at a time, into the
<code>SANDBOX</code>:</p>
<div class="highlight">
<pre>
<span class="c1"># This is an empty array</span>
<span class="nb">puts</span> <span class="o">[].</span><span class=
"n">inspect</span>
<span class=
"c1"># This is an array with a single element, the string "apples"</span>
<span class="nb">puts</span> <span class="o">[</span><span class=
"s2">"apples"</span><span class="o">].</span><span class="n">inspect</span>
<span class=
"c1"># This is an array with three elements: integers 1, 2, and 3</span>
<span class="nb">puts</span> <span class="o">[</span><span class=
"mi">1</span><span class="p">,</span><span class="mi">2</span><span class=
"p">,</span><span class="mi">3</span><span class="o">].</span><span class=
"n">inspect</span>
<span class="c1"># This is an array of all the months</span>
<span class="nb">puts</span> <span class="o">[</span><span class=
"s2">"January"</span><span class="p">,</span> <span class=
"s2">"February"</span><span class="p">,</span> <span class=
"s2">"March"</span><span class="p">,</span> <span class=
"s2">"April"</span><span class="p">,</span> <span class=
"s2">"May"</span><span class="p">,</span> <span class=
"s2">"June"</span><span class="p">,</span> <span class=
"s2">"July"</span><span class="p">,</span> <span class=
"s2">"August"</span><span class="p">,</span> <span class=
"s2">"September"</span><span class="p">,</span> <span class=
"s2">"October"</span><span class="p">,</span> <span class=
"s2">"November"</span><span class="p">,</span> <span class=
"s2">"December"</span><span class="o">].</span><span class="n">inspect</span>
<span class=
"c1"># The elements of an array don't all need to be the same type.</span>
<span class="c1"># In fact, that can be any type.</span>
<span class="nb">puts</span> <span class="o">[</span><span class=
"mi">10</span><span class="p">,</span> <span class=
"s2">"cats"</span><span class="p">,</span> <span class=
"mi">4</span><span class="o">.</span><span class="mi">5</span><span class=
"p">,</span> <span class="s2">"piano"</span><span class=
"o">].</span><span class="n">inspect</span>
<span class="c1"># We can use variables in arrays, too.</span>
<span class="n">dummy</span> <span class="o">=</span> <span class=
"s2">"foobar"</span>
<span class="nb">puts</span> <span class="o">[</span><span class=
"mi">1</span><span class="p">,</span> <span class="mi">2</span><span class=
"p">,</span> <span class="n">dummy</span><span class="o">].</span><span class=
"n">inspect</span> <span class="c1"># => [1, 2, "foobar"]</span>
</pre>
</div>
<h3 id="toc_7">Reading from Arrays</h3>
<p>Let's take a look at that array of months from above. Paste
the following into the <code>SANDBOX</code>.</p>
<div class="highlight">
<pre>
<span class="n">months</span> <span class="o">=</span> <span class=
"o">[</span><span class="s2">"January"</span><span class=
"p">,</span> <span class="s2">"February"</span><span class=
"p">,</span> <span class="s2">"March"</span><span class="p">,</span>
<span class="s2">"April"</span><span class="p">,</span> <span class=
"s2">"May"</span><span class="p">,</span> <span class=
"s2">"June"</span><span class="p">,</span>
<span class="s2">"July"</span><span class="p">,</span> <span class=
"s2">"August"</span><span class="p">,</span> <span class=
"s2">"September"</span><span class="p">,</span>
<span class="s2">"October"</span><span class=
"p">,</span> <span class="s2">"November"</span><span class=
"p">,</span> <span class="s2">"December"</span><span class="o">]</span>
</pre>
</div>
<p>(Yes, you can split up array declarations across multiple
lines, so long as the line ends with a comma.)</p>
<p>Once we have data in an array, how do we get data out of an
array? We do it by <em>indexing</em> the array, a fancy way of
telling the array: "Give me the element at <em>this</em>
position."</p>
<p>Make sure you've entered in the code that defines the
<code>months</code> array above, then type the following into
the <code>SANDBOX</code>.</p>
<div class="highlight">
<pre>
<span class=
"c1"># You "index into" an array using the bracket [...] notation.</span>
<span class="c1"># The index (an integer) goes between the brackets</span>
<span class="c1"># What's at index 0?</span>
<span class="nb">puts</span> <span class="n">months</span><span class=
"o">[</span><span class="mi">0</span><span class="o">]</span>
</pre>
</div>
<p>This should print out <code>"January"</code>.</p>
<p>If you see the error below it means you didn't paste in the
code to define the <code>months</code> array. Ruby will raise
an error if you try to access a variable before you define
it.</p>
<div class="highlight">
<pre>
undefined local variable or method `months' for main
</pre>
</div>
<h3 id="toc_8">Counting from 0</h3>
<p>It probably strikes you as odd that <code>"January"</code>,
the <em>first</em> element of the <code>months</code> array, is
at <code>months[0]</code> and not <code>months[1]</code>. If it
doesn't, seek medical attention immediately: you might be a
computer.</p>
<p>Computers and most programming languages count from
<code>0</code> rather than <code>1</code>. So the first element
of an array is at index <code>0</code>, the second is at index
<code>1</code>, etc. There are good reasons for this, but for
now you'll just have to get used to it.</p>
<h3 id="toc_9">Changing Arrays</h3>
<p>We can change the values in an array, too.</p>
<div class="highlight">
<pre>
<span class="n">array</span> <span class="o">=</span> <span class=
"o">[</span><span class="mi">1</span><span class="p">,</span><span class=
"mi">2</span><span class="p">,</span><span class="mi">3</span><span class=
"o">]</span>
<span class="nb">puts</span> <span class="s2">"array[2] is </span><span class=
"si">#{</span><span class="n">array</span><span class="o">[</span><span class=
"mi">2</span><span class="o">]</span><span class="si">}</span><span class=
"s2">"</span>
<span class="n">array</span><span class="o">[</span><span class=
"mi">2</span><span class="o">]</span> <span class="o">=</span> <span class=
"s2">"apples"</span>
<span class="nb">puts</span> <span class=
"s2">"array[2] is now </span><span class="si">#{</span><span class=
"n">array</span><span class="o">[</span><span class="mi">2</span><span class=
"o">]</span><span class="si">}</span><span class="s2">"</span>
</pre>
</div>
<h3 id="toc_10">Array Vocabulary</h3>
<p>It's worth learning some vocabulary around arrays, just to
get comfortable. Using the <code>months</code> array, again,
the following statements are all true:</p>
<ul>
<li>The string <code>"January"</code> is the first element
of the array <code>months</code></li>
<li>The string <code>"January"</code> is at index
<code>0</code></li>
<li>The string <code>"December"</code> is the last element
of the array <code>months</code></li>
<li>The string <code>"December"</code> is at index
<code>11</code></li>
<li>The array <code>months</code> has <code>12</code>
elements</li>
<li>The value at index <code>3</code> is the string
<code>"April"</code>, which is the fourth element</li>
</ul>
</div>
<div class="span8 offset1" id="lesson_content">
<h2>Array#each</h2>
<p>So far we've been writing code like this to loop through an
<code>Array</code>:</p>
<div class="highlight">
<pre>
<span class="n">names</span> <span class="o">=</span> <span class=
"o">[</span><span class="s2">"Alex"</span><span class="p">,</span> <span class=
"s2">"Cassie"</span><span class="p">,</span> <span class=
"s2">"Stephanie"</span><span class="o">]</span>
<span class="n">names</span><span class="o">.</span><span class=
"n">length</span><span class="o">.</span><span class=
"n">times</span> <span class="k">do</span> <span class="o">|</span><span class=
"n">i</span><span class="o">|</span>
<span class="nb">puts</span> <span class="n">names</span><span class=
"o">[</span><span class="n">i</span><span class="o">]</span>
<span class="k">end</span>
</pre>
</div>
<p>The variable <code>i</code> isn't playing much of a role
here. Most of the time we don't actually care what its value
is. We're only using it to get at the values we want.</p>
<p>In other words, what we're really saying isn't "Run this
block of code as many times as there are elements in
<code>Array</code>", but "Run this block of code once for each
element in <code>Array</code>."</p>
<p>Ruby lets us express this more directly using
<code>Array#each</code>:</p>
<div class="highlight">
<pre>
<span class="n">names</span> <span class="p">=</span> <span class=
"p">[</span>"<span class="n">Alex</span>"<span class="p">,</span> "<span class=
"n">Cassie</span>"<span class="p">,</span> "<span class=
"n">Stephanie</span>"<span class="p">]</span>
<span class="n">names</span><span class="p">.</span><span class=
"n">each</span> <span class="n">do</span> <span class="o">|</span><span class=
"n">name</span><span class="o">|</span>
<span class="n">puts</span> <span class="n">name</span>
<span class="k">end</span>
</pre>
</div>
<p>In the same way that <code>names.length.times do |i| ...
end</code> hands the block of code the current index of the
<code>Array</code> and then runs it, <code>names.each do |name|
... end</code> hands the block of code the current
<em>element</em> of the <code>Array</code> and the runs it.</p>
<p>We don't need that silly <code>i</code> at all most of the
time, and using <code>Array#each</code> more directly expresses
what we want to do (in addition to reading more like everyday
English).</p>
<h3 id="toc_0">Examples</h3>
<p>Let's re-write the examples from the previous lesson using
<code>Array#each</code> instead of
<code>Integer#times</code>.</p>
<h4 id="toc_1">array_sum</h4>
<p>Here's <code>array_sum</code>:</p>
<div class="highlight">
<pre>
<span class="k">def</span> <span class="nf">array_sum</span><span class=
"p">(</span><span class="n">array</span><span class="p">)</span>
<span class="n">sum</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">array</span><span class="o">.</span><span class=
"n">each</span> <span class="k">do</span> <span class="o">|</span><span class=
"n">element</span><span class="o">|</span>
<span class="n">sum</span> <span class="o">+=</span> <span class=
"n">element</span>
<span class="k">end</span>
<span class="n">sum</span>
<span class="k">end</span>
<span class="nb">puts</span> <span class="n">array_sum</span><span class=
"p">(</span><span class="o">[</span><span class="mi">10</span><span class=
"p">,</span> <span class="mi">20</span><span class="p">,</span> <span class=
"mi">30</span><span class="p">,</span> <span class="mi">100</span><span class=
"o">]</span><span class="p">)</span>
<span class="nb">puts</span> <span class="n">array_sum</span><span class=
"p">(</span><span class="o">[]</span><span class="p">)</span>
<span class="nb">puts</span> <span class="n">array_sum</span><span class=
"p">(</span><span class="o">[-</span><span class="mi">1</span><span class=
"p">,</span> <span class="mi">1</span><span class="o">]</span><span class=
"p">)</span>
</pre>
</div>
<p>This will print out:</p>
<div class="highlight">
<pre>
160
0
0
</pre>
</div>
<h4 id="toc_2">array_reverse</h4>
<p>Here's <code>array_reverse</code>:</p>
<div class="highlight">
<pre>
<span class="k">def</span> <span class="nf">array_reverse</span><span class=
"p">(</span><span class="n">array</span><span class="p">)</span>
<span class="n">results</span> <span class="o">=</span> <span class=
"o">[]</span>
<span class="n">array</span><span class="o">.</span><span class=
"n">each</span> <span class="k">do</span> <span class="o">|</span><span class=
"n">item</span><span class="o">|</span>
<span class=
"c1"># Array#push appends an element to the end of the Array</span>
<span class=
"c1"># So we append the reversed strings to the new array, </span>
<span class="c1"># one at a time</span>
<span class="n">results</span><span class="o">.</span><span class=
"n">push</span><span class="p">(</span><span class="n">item</span><span class=
"o">.</span><span class="n">reverse</span><span class="p">)</span>
<span class="c1"># You could also call:</span>
<span class="c1"># results << item.reverse</span>
<span class="k">end</span>
<span class="n">results</span>
<span class="k">end</span>
<span class="nb">puts</span> <span class="s2">"Some examples..."</span>
<span class="nb">puts</span> <span class="n">array_reverse</span><span class=
"p">(</span><span class="o">[</span><span class=
"s1">'racecar'</span><span class="o">]</span><span class=
"p">)</span><span class="o">.</span><span class="n">inspect</span>
<span class="nb">puts</span> <span class="n">array_reverse</span><span class=
"p">(</span><span class="o">[</span><span class=
"s1">'Nancy Drew'</span><span class="p">,</span> <span class=
"s1">'Frank Hardy'</span><span class="o">]</span><span class=
"p">)</span><span class="o">.</span><span class="n">inspect</span>
<span class="n">my_array</span> <span class="o">=</span> <span class=
"o">[</span><span class="s1">'pineapple'</span><span class=
"p">,</span> <span class="s1">'mango'</span><span class=
"p">,</span> <span class="s1">'coconut'</span><span class="o">]</span>
<span class="nb">puts</span> <span class="s2">""</span>
<span class="nb">puts</span> <span class=
"s2">"What if we call array_reverse twice, on itself?"</span>
<span class="nb">puts</span> <span class="n">array_reverse</span><span class=
"p">(</span><span class="n">my_array</span><span class="p">)</span><span class=
"o">.</span><span class="n">inspect</span>
<span class="nb">puts</span> <span class="n">array_reverse</span><span class=
"p">(</span><span class="n">array_reverse</span><span class=
"p">(</span><span class="n">my_array</span><span class=
"p">))</span><span class="o">.</span><span class="n">inspect</span>
</pre>
</div>
<h4 id="toc_3">add_ten_to_each</h4>
<p>Here's <code>add_ten_to_each</code>:</p>
<div class="highlight">
<pre>
<span class="k">def</span> <span class="nf">add_ten_to_each</span><span class=
"p">(</span><span class="n">array</span><span class="p">)</span>
<span class="n">new_array</span> <span class="o">=</span> <span class=
"o">[]</span>
<span class="n">array</span><span class="o">.</span><span class=
"n">each</span> <span class="k">do</span> <span class="o">|</span><span class=
"n">element</span><span class="o">|</span>
<span class="n">new_array</span><span class="o">.</span><span class=
"n">push</span><span class="p">(</span><span class=
"n">element</span> <span class="o">+</span> <span class=
"mi">10</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">new_array</span>
<span class="k">end</span>
<span class="n">my_array</span> <span class="o">=</span> <span class=
"o">[</span><span class="mi">1</span><span class="p">,</span><span class=
"mi">2</span><span class="p">,</span><span class="mi">3</span><span class=
"p">,</span><span class="mi">4</span><span class="p">,</span><span class=
"mi">5</span><span class="o">]</span>
<span class="nb">puts</span> <span class="s2">"my_array is </span><span class=
"si">#{</span><span class="n">my_array</span><span class=
"o">.</span><span class="n">inspect</span><span class="si">}</span><span class=
"s2">"</span>
<span class="c1"># other_array is now equal to [11, 12, 13, 14, 15]</span>
<span class="n">other_array</span> <span class="o">=</span> <span class=
"n">add_ten_to_each</span><span class="p">(</span><span class=
"n">my_array</span><span class="p">)</span>
<span class="nb">puts</span> <span class=
"s2">"other_array is now </span><span class="si">#{</span><span class=
"n">other_array</span><span class="o">.</span><span class=
"n">inspect</span><span class="si">}</span><span class="s2">"</span>
</pre>
</div>
</div>
<div class="span8 offset1" id="lesson_content">
<h2>What are Objects and Classes?</h2>
<p>We've been talking a lot about objects and classes, but what
are they?</p>
<h3 id="toc_0">Objects</h3>
<p>Objects are bundles of data and the methods that read,
write, change, or otherwise act on that data.</p>