-
Notifications
You must be signed in to change notification settings - Fork 71
/
index.html
1359 lines (1263 loc) · 43.5 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
---
layout: default
---
<h1>Cheat Sheet Conventions</h1>
<p>
<strong>Bold</strong> words are what is really important e.g.
the command and concept shown in the usage category. In the code usage and
example columns these highlight the main part of the concept, like this:
<code>general_stuff<strong>.concept</strong></code>.
In the same columns <code><em>italic_words</em></code> mark the
arguments/parameters of a command/method.
</p>
<p>
However <em>italic words</em> in the descriptions or general text denote
more general concepts or concepts explained elsewhere in this cheat
sheet or in general.
</p>
<h1 id="console">Console Basics</h1>
<p>
The console (also called <em>command line, command prompt or
terminal</em>) is just another way of interacting with your computer.
So you can basically do anything with it that you could also do with
your graphical desktop user interface. This sections contains a couple
of examples.
</p>
<p>
For the different operating systems <strong>starting the console</strong>
differs.
</p>
<ul>
<li><strong>Windows:</strong> Open the start menu and search for
command prompt. Alternatively choose <em>execute</em> and enter
<em>cmd</em>.</li>
<li><strong>Mac:</strong> Open Spotlight, type <em>terminal</em>, and
start that program.
</li>
<li><strong>Linux:</strong> The terminal should be one of the main
options once you open the main menu of your distribution. Otherwise
search for <em>terminal</em> if your distribution has such an option
or look under Accessories.
</li>
</ul>
<table class="table table-striped">
<tr>
<th class="concept">Concept</th>
<th class="usage">Usage</th>
<th class="example">Examples</th>
<th class="description">Description</th>
</tr>
<tr>
<td>Change directory</td>
<td>
<code><strong>cd</strong> <em>directory</em></code>
</td>
<td>
<p><code>cd my_app</code></p>
<p><code>cd my_app/app/controllers</code></p>
</td>
<td>Changes the directory to the specified directory on the
console.
</td>
</tr>
<tr>
<td>List contents directory</td>
<td>
<p><code><strong>ls</strong> <em>directory</em></code></p>
<p><code>Windows: <strong>dir</strong> <em>directory</em></code>
</p>
</td>
<td>
<p><code><strong>ls</strong></code></p>
<p><code><strong>ls</strong> <em>my_app</em></code></p>
</td>
<td>Shows all contents (files and folders) of the directory.
If no directory is specified shows the contents of the
current directory.
</td>
</tr>
<tr>
<td>Directory you are currently in</td>
<td><code><strong>pwd</strong></code></td>
<td><code><strong>pwd</strong></code></td>
<td>Shows the full path of the directory you are currently
in. E.g. /home/tobi/railsgirls
<br />
A note on filenames: if a file or directory name starts with a slash / as in the output of pwd above, it is an absolute filename specifying the complete filename starting at the root of the current file system (e.g. hard disk). If the slash (/) is omitted, the file name is relative to the current working directory.
</td>
</tr>
<tr>
<td>Create a new directory</td>
<td><code><strong>mkdir</strong> <em>name</em></code></td>
<td>
<p><code><strong>mkdir</strong> <em>rails</em></code></p>
<p><code><strong>mkdir</strong> <em>fun</em></code></p>
</td>
<td>
Creates a directory with the given name in the folder you are currently
in.
</td>
</tr>
<tr>
<td>Delete a file</td>
<td>
<p><code><strong>rm</strong> <em>file</em></code></p>
<p><code>Windows: <strong>del</strong> <em>file</em></code>
</td>
<td>
<p><code><strong>rm</strong> <em>foo</em></code></p>
<p><code><strong>rm</strong> <em>index.html</em></code></p>
<p><code><strong>rm</strong> <em>pictures/old_picture.jpg</em></code></p>
</td>
<td>
<p>Deletes the specified file. Be <strong>extra cautious</strong> with this
as it would be too bad to delete something you still need :-(</p>
<p>
You can simply specify the name of a file of the directory you are
currently in. However you can also specify a path, this is shown in the
third example. There we delete the <em>old_picture.jpg</em> file from
the <em>pictures</em> folder.
</p>
</td>
</tr>
<tr>
<td>Delete a directory</td>
<td>
<p><code><strong>rm -r</strong> <em>folder</em></code></p>
<p><code>Windows: <strong>rd</strong> <em>folder</em></code>
</td>
<td>
<p><code><strong>rm -r</strong> <em>stuff_i_dont_need</em></code></p>
<p><code><strong>rm -r</strong> <em>stuff_i_dont_need/</em></code></p>
<p><code><strong>rm -r</strong> <em>old_application</em></code></p>
</td>
<td>
<p>Deletes the specified folder <strong>and all of its contents</strong>.
So please be <strong>super cautious</strong> with this! Make sure that
you do not need any of the contents of this folder any more.
</p>
<p>
So why would you want to delete a whole folder? Well maybe it was an old
application that you do not need anymore :-)
</p>
</td>
</tr>
<tr>
<td>Starting a program</td>
<td><code><strong>program</strong> <em>arguments</em></code></td>
<td>
<p><code><strong>firefox</strong></code></p>
<p><code>
<strong>firefox</strong> <em>railsgirlsberlin.de</em></code>
</p>
<p><code><strong>irb</strong></code></p>
</td>
<td>Starts the program with the given name and arbitrary arguments
if the program takes arguments. Firefox is just one example. Starting
Firefox without arguments just opens up Firefox. If you give it an
argument it opens the specified URL.
When you type in <code>irb</code> this starts <em>interactive ruby</em>.
</td>
</tr>
<tr>
<td>Abort the program</td>
<td>Press <strong>Ctrl + C</strong></td>
<td>-</td>
<td>This will abort the program currently running in the terminal.
For instance this is used to shut down the Rails server.
You can also abort many other related tasks with it, including:
bundle install, rake db:migrate, git pull and many more!
</td>
</tr>
</table>
<h1 id="ruby">Ruby Basics</h1>
<p>
Ruby is the programming language Ruby on Rails is written in. So most
of the time you will be writing Ruby code. Therefore it is good to
grasp the basics of Ruby. If you just want to play with Ruby, type
<strong>irb</strong> into your console to start interactive ruby. There
you can easily experiment with Ruby. To leave irb, type
<strong>exit</strong>.
</p>
<p>
This is just a very small selection of concepts. This is especially
true later on when we talk about what Arrays, Strings etc. can do.
For more complete information have a look at
<a href="http://ruby-doc.org/">ruby-doc</a> or search with your favorite
search engine!
</p>
<h2 id="ruby-concepts">General concepts</h2>
<table class="table table-striped">
<tr>
<th class="concept">Concept</th>
<th class="usage">Usage</th>
<th class="example">Examples</th>
<th class="description">Description</th>
</tr>
<tr>
<td>Comment</td>
<td>
<code><strong>#</strong> <em>Comment text</em></code>
</td>
<td>
<p>
<code><strong>#</strong> <em>This text is a comment</em></code>
</p>
<p>
<code>some.ruby_code <strong>#</strong> A comment</code>
</p>
<p>
<code><strong>#</strong> some.ignored_ruby_code</code>
</p>
</td>
<td>
Ruby ignores everything that is marked as a comment. It does not try
to execute it. Comments are just there for you as information.
Comments are also commonly used to <em>comment out code</em>. That
is when you don't want some part of your code to execute
but you don't want to delete it just yet, because you are trying
different things out.
</td>
</tr>
<tr>
<td>Variables</td>
<td>
<code><strong>variable =</strong> <em>some_value</em></code>
</td>
<td>
<pre><code><strong>name =</strong> <em>"Tobi"</em>
<strong>name</strong> # => "Tobi"</code></pre>
<pre><code><strong>sum =</strong> <em>18 + 5</em>
<strong>sum</strong> # => 23</code></pre>
</td>
<td>
With a variable you tell Ruby that from now on you want to refer to
that value by the name you gave it. So for the first example, from
now on when you use <em>name</em> Ruby will know that you meant
<em>"Tobi"</em>.
</td>
</tr>
<tr>
<td>Console output</td>
<td>
<code><strong>puts</strong> <em>something</em></code>
</td>
<td>
<p><code><strong>puts</strong> <em>"Hello World"</em></code></p>
<p><code><strong>puts</strong> <em>[1, 5, "mooo"]</em></code></p>
</td>
<td>
Prints its argument to the console. Can be used in Rails apps
to print something in the console where the server is running.
</td>
</tr>
<tr>
<td>Call a method</td>
<td>
<code>object<strong>.method</strong><em>(arguments)</em></code>
</td>
<td>
<p><code>string<strong>.length</strong></code></p>
<p><code>
array<strong>.delete_at</strong><em>(2)</em>
</code></p>
<p><code>string<strong>.gsub</strong><em>("ae", "ä")</em></code></p>
</td>
<td>
<p>Calling a method is also often referred to as
<em>sending a message</em> in Ruby. Basically we are sending an
object some kind of message and are waiting for its response.
This message may have no arguments or multiple arguments, depending
on the message.
So we kindly ask the object to do something or give us some
information.
When you "call a method" or "send a message" something happens. In the
first example we ask a String how long it is (how many
characters it consists of). In the last example we substitute all
occurrences of "ae" in the string with the German "ä".</p>
<p>
Different kinds of objects (Strings, Numbers, Arrays...)
understand different messages.
</p>
</td>
</tr>
<tr>
<td>Define a method</td>
<td>
<pre><code><strong>def</strong> <em>name(parameter)</em>
<em># method body</em>
<strong>end</strong></code></pre>
</td>
<td>
<pre><code><strong>def</strong> <em>greet(name)</em>
<em>puts "Hi there " + name</em>
<strong>end</strong></code></pre>
</td>
<td>
<p>Methods are basically reusable units of behaviour. And you can
define them yourself just like this. Methods are small and focused
on implementing a specific behaviour.</p>
<p>
Our example method is focused on greeting people.
You could call it like this: <code>greet("Tobi")</code>
</p>
</td>
</tr>
<tr>
<td>Equality</td>
<td><code>object <strong>==</strong> other</code></td>
<td>
<p><code>true <strong>==</strong> true # => true</code></p>
<p><code>3 <strong>==</strong> 4 # => false</code></p>
<p><code>"Hello" <strong>==</strong> "Hello" # => true</code></p>
<p><code>"Helo" <strong>==</strong> "Hello" # => false</code></p>
</td>
<td>
With two equal signs you can check if two things are the
same. If so, <code>true</code> will be returned; otherwise, the result
will be <code>false</code>.
</td>
</tr>
<tr>
<td>Inequality</td>
<td><code>object <strong>!=</strong> other</code></td>
<td>
<p><code>true <strong>!=</strong> true # => false</code></p>
<p><code>3 <strong>!=</strong> 4 # => true</code></p>
</td>
<td>
Inequality is the inverse to equality, e.g. it results in
<code>true</code> when two values are not the same and it results
in <code>false</code> when they are the same.
</td>
</tr>
<tr>
<td>Decisions with if</td>
<td>
<pre><code><strong>if</strong> <em>condition</em>
# happens when true
<strong>else</strong>
# happens when false
<strong>end</strong>
</code></pre>
</td>
<td>
<pre><code><strong>if</strong> <em>input == password</em>
grant_access
<strong>else</strong>
deny_access
<strong>end</strong>
</code></pre>
</td>
<td>
<p>With if-clauses you can decide based upon a <em>condition</em>
what to do. When the condition is considered true, then the code
after it is executed. If it is considered false, the code after
the "else" is executed.</p>
<p>
In the example, access is granted based upon the decision if a
given input matches the password.
</p>
</td>
</tr>
<tr>
<td>Constants</td>
<td><code><strong>CONSTANT =</strong> <em>some_value</em></code></td>
<td>
<pre><code><strong>PI = 3.1415926535</strong>
<strong>PI</strong> # => 3.1415926535</code></pre>
<pre><code><strong>ADULT_AGE</strong> = 18
<strong>ADULT_AGE</strong> # => 18</code></pre>
</td>
<td>
Constants look like variables, just in UPCASE. Both hold
values and give you a name to refer to those values. However while
the value a variable holds may change or might be of an unknown
value (if you save user input in a variable) constants are
different. They have a known value that should never change. Think
of it a bit like mathematical or physical constants. These don't
change, they always refer to the same value.
</td>
</tr>
</table>
<h2 id="ruby-numbers">Numbers</h2>
<p>
Numbers are what you would expect them to be, normal
numbers that you use to perform basic math operations.
</p>
<p>
More information about numbers can be found in the
<a href="http://ruby-doc.org/core-2.0/Numeric.html">ruby-doc of Numeric</a>.
</p>
<table class="table table-striped">
<tr>
<th class="concept">Concept</th>
<th class="usage">Usage</th>
<th class="example">Examples</th>
<th class="description">Description</th>
</tr>
<tr>
<td>normal Number</td>
<td><code><strong>number_of_your_choice</strong></code></td>
<td>
<p><code><strong>0</strong></code></p>
<p><code><strong>-11</strong></code></p>
<p><code><strong>42</strong></code></p>
</td>
<td>
Numbers are natural for Ruby, you just have to enter them!
</td>
</tr>
<tr>
<td>Decimals</td>
<td><code><strong>main.decimal</strong></code></td>
<td>
<p><code><strong>3.2</strong></code></p>
<p><code><strong>-5.0</strong></code></p>
</td>
<td>
You can achieve decimal numbers in Ruby simply by adding a point.
</td>
</tr>
<tr>
<td>Basic Math</td>
<td>
<code>n <strong>operator</strong> <em>m</em></code>
</td>
<td>
<p><code>2 <strong>+</strong> <em>3</em> # => 5</code></p>
<p><code>5 <strong>-</strong> <em>7</em> # => -2</code></p>
<p><code>8 <strong>*</strong> <em>7</em> # => 56</code></p>
<p><code>84 <strong>/</strong> <em>4</em> # => 21</code></p>
</td>
<td>
In Ruby you can easily use basic math operations.
In that sense you may use Ruby as a super-powered calculator.
</td>
</tr>
<tr>
<td>Comparison</td>
<td>
<code>n <strong>operator</strong> <em>m</em></code>
</td>
<td>
<p><code>12 <strong>></strong> <em>3</em> # => true</code></p>
<p><code>12 <strong><</strong> <em>3</em> # => false</code></p>
<p><code>7 <strong>>=</strong> <em>7</em> # => true</code></p>
</td>
<td>
<p>Numbers may be compared to determine if a number is bigger or smaller
than another number. When you have the age of a person saved in the
<code>age</code> variable you can see if that person is considered
an adult in Germany:</p>
<p><code>age >= 18 # true or false</code></p>
</td>
</tr>
</table>
<h2 id="ruby-strings">Strings</h2>
<p>
Strings are used to hold textual information. They may contain
single characters, words, sentences or a whole book. However you may
just think of them as an ordered collection of characters.
</p>
<p>
You can find out more about Strings at the
<a href="http://ruby-doc.org/core-2.0/String.html">ruby-doc page about
Strings</a>.
</p>
<table class="table table-striped">
<tr>
<th class="concept">Concept</th>
<th class="usage">Usage</th>
<th class="example">Examples</th>
<th class="description">Description</th>
</tr>
<tr>
<td>Create</td>
<td>
<p><code><strong>'</strong>A string<strong>'</strong></code></p>
</td>
<td>
<p><code><strong>'</strong>Hello World<strong>'</strong></code></p>
<p><code><strong>'</strong>a<strong>'</strong></code></p>
<p><code>
<strong>'</strong>Just characters 129 _!$%^<strong>'</strong>
</code></p>
<p><code><strong>''</strong></code></p>
</td>
<td>
A string is created by putting quotation marks around a
character sequence. A <a href="https://github.com/bbatsov/ruby-style-guide/">Ruby style guide</a>
recommends using single quotes for simple strings.
</td>
</tr>
<tr>
<td>Interpolation</td>
<td>
<p><code><strong>"</strong>A string and an #{expression}<strong>"</strong></code></p>
</td>
<td>
<p><code><strong>"</strong>Email: #{user.email}<strong>"</strong></code></p>
<p><code><strong>"</strong>The total is #{2 + 2}<strong>"</strong></code></p>
<p><code><strong>"</strong>A simple string<strong>"</strong></code></p>
</td>
<td>
You can combine a string with a variable or Ruby expression using double quotation marks.
This is called "interpolation." It is okay to use double quotation marks around a simple
string, too.
</td>
</tr>
<tr>
<td>Length</td>
<td><code>string<strong>.length</strong></code></td>
<td>
<p><code>"Hello"<strong>.length</strong> # => 5</code></p>
<p><code>""<strong>.length</strong> # => 0</code></p>
</td>
<td>
You can send a string a message, asking it how long it is and it
will respond with the number of characters it consists of. You could
use this to check if the desired password of a user exceeds the
required minimum length. Notice how we add a comment to show the
expected result.
</td>
</tr>
<tr>
<td>Concatenate</td>
<td><code>string <strong>+</strong> <em>string2</em></code></td>
<td>
<pre><code>"Hello " <strong>+</strong> <em>"reader"</em>
# => "Hello reader"</code></pre>
<p><code>"a" <strong>+</strong> <em>"b"</em> <strong>+</strong>
<em>"c"</em>
# => "abc"</code></p>
</td>
<td>Concatenates two or more strings together and returns the result.</td>
</tr>
<tr>
<td>Substitute</td>
<td>
<pre><code>string<strong>.gsub</strong><em>(a_string,
substitute)</em></code></pre>
</td>
<td>
<pre><code>"Hae"<strong>.gsub</strong><em>("ae", "ä")
# => "Hä"</em></code></pre>
<pre><code>"Hae"<strong>.gsub</strong><em>("b", "ä")
# => "Hae"</em></code></pre>
<pre><code>"Greenie"<strong>.gsub</strong><em>("e", "u")
# => "Gruuniu"</em></code></pre>
</td>
<td><em>gsub</em> stands for "globally substitute". It substitutes all
occurrences of <code>a_string</code> within the string with
<code>substitute</code>.</td>
</tr>
<tr>
<td>Access</td>
<td>
<code>
string<strong>[</strong><em>position</em><strong>]</strong>
</code>
</td>
<td>
<code>
"Hello"<strong>[</strong><em>1</em><strong>]</strong> # => "e"
</code>
</td>
<td>
Access the character at the given position in the string. Be aware
that the first position is actually position <em>0</em>.
</td>
</tr>
</table>
<h2 id="ruby-arrays">Arrays</h2>
<p>
An array is an ordered collection of items which is indexed by numbers.
So an array contains multiple objects that are mostly related to each
other. So what could you do? You could store a collection of the names
of your favorite fruits and name it <em>fruits</em>.
</p>
<p>
This is just a small selection of things an Array can do. For more
information have a look at the
<a href=http://ruby-doc.org/core-2.0/Array.html>ruby-doc for Array</a>.
</p>
<table class="table table-striped">
<tr>
<th class="concept">Concept</th>
<th class="usage">Usage</th>
<th class="example">Examples</th>
<th class="description">Description</th>
</tr>
<tr>
<td>Create</td>
<td>
<code><strong>[</strong><em>contents</em><strong>]</strong></code>
</td>
<td>
<p><code><strong>[]</strong></code></p>
<p><code>
<strong>[</strong><em>"Rails", "fun", 5</em><strong>]</strong>
</code></p>
</td>
<td>Creates an Array, empty or with the specified contents.</td>
</tr>
<tr>
<td>Number of elements</td>
<td>
<code>array<strong>.size</strong></code>
</td>
<td>
<p><code>[]<strong>.size</strong> # => 0</code></p>
<p><code>[1, 2, 3]<strong>.size</strong> # => 3</code></p>
<p><code>["foo", "bar"]<strong>.size</strong> # => 2</code></p>
</td>
<td>Returns the number of elements in an Array.</td>
</tr>
<tr>
<td>Access</td>
<td>
<code>
array<strong>[</strong><em>position</em><strong>]</strong>
</code>
</td>
<td>
<pre><code>array = ["hi", "foo", "bar"]
array<strong>[</strong><em>0</em><strong>]</strong> # => "hi"
array<strong>[</strong><em>2</em><strong>]</strong> # => "bar"</code></pre>
</td>
<td>As an Array is a collection of different elements, you often want
to access a single element of the Array.
Arrays are indexed by numbers so you can use a number
to access an individual element. Be aware that the
numbering actually starts with "0" so the first element
actually is the 0th. And the last element of a three element
array is element number 2.
</td>
</tr>
<tr>
<td>Adding an element</td>
<td>
<code>array <strong><<</strong> <em>element</em></code>
</td>
<td>
<pre><code>array = [1, 2, 3]
array <strong><<</strong> <em>4</em>
array # => [1, 2, 3, 4]
</code></pre>
</td>
<td>
Adds the element to the end of the array,
increasing the size of the array by one.
</td>
</tr>
<tr>
<td>Assigning</td>
<td>
<code>array<strong>[</strong><em>number</em><strong>] =
</strong> <em>value</em></code>
</td>
<td>
<pre><code>array = ["hi", "foo", "bar"]
array<strong>[</strong><em>2</em><strong>] =</strong> <em>"new"</em>
array # => ["hi", "foo", "new"]</code></pre>
</td>
<td>Assigning new Array Values works a lot like accessing
them; use an equals sign to set a new value. Voila!
You changed an element of the array! Weehuuuuu!
</td>
</tr>
<tr>
<td>Delete at index</td>
<td><code>
array<strong>.delete_at</strong><em>(i)</em>
</code></td>
<td>
<pre><code>array = [0, 14, 55, 79]
array<strong>.delete_at</strong><em>(2)</em>
array # => [0, 14, 79]
</code></pre>
</td>
<td>
Deletes the element of the array at the specified index. Remember
that indexing starts at 0. If you specify an index larger than the
number of elements in the array, nothing will happen.
</td>
</tr>
<tr>
<td>Iterating</td>
<td>
<code>array<strong>.each</strong> <em>do |e| .. end</em></code>
</td>
<td>
<p><code>
persons<strong>.each</strong> <em>do |p| puts p.name end</em>
</code></p>
<p><code>
numbers<strong>.each</strong> <em>do |n| n = n * 2 end</em>
</code></p>
</td>
<td>
<p>
"Iterating" means doing something for <em>each</em> element
of the array. Code placed between <em>do</em> and <em>end</em>
determines what is done to each element in the array.
</p>
<p>
The first example prints the name of every person in the array to
the console. The second example simply doubles every number of a
given array.
</p>
</td>
</tr>
</table>
<h2 id="ruby-hashes">Hashes</h2>
<p>
Hashes associate a <em>key</em> to some <em>value</em>. You may then retrieve the value based upon its key.
This construct is called a <em>dictionary</em> in other languages, which is appropriate
because you use the key to "look up" a value, as you would look up a definition for
a word in a dictionary. Each key must be unique for a given hash but values can be repeated.
</p>
<p>
Hashes can map from anything to anything! You can map from Strings to
Numbers, Strings to Strings, Numbers to Booleans... and you can mix
all of those! Although it is common that at least all the keys are of
the same class. <em>Symbols</em> are especially common as keys. Symbols
look like this: <code>:symbol</code>. A symbol is a colon followed by some
characters. You can think of them as special strings that stand for
(symbolize) something! We often use symbols because
Ruby runs faster when we use symbols instead of strings.
</p>
<p>Learn more about hashes at
<a href="http://www.ruby-doc.org/core-2.0/Hash.html">ruby-doc</a>.
</p>
<table class="table table-striped">
<tr>
<th class="concept">Concept</th>
<th class="usage">Usage</th>
<th class="hash-example">Examples</th>
<th class="description">Description</th>
</tr>
<tr>
<td>Creating</td>
<td><code>
<strong>{</strong>key <strong>=></strong> value<strong>}</strong>
</code></td>
<td>
<p><code>
<strong>{</strong>:hobby <strong>=></strong>
"programming"<strong>}</strong>
</code></p>
<pre><code><strong>{</strong>42 <strong>=></strong> "answer", "score" <strong>=></strong> 100,
:name <strong>=></strong> "Tobi"<strong>}</strong>
</code></pre>
</td>
<td>
You create a hash by surrounding the key-value pairs with curly
braces. The arrow always goes from the <em>key</em> to the
<em>value</em> depicting the meaning: <em>"This key points to this
value."</em>. Key-value pairs are then separated by commas.
</td>
</tr>
<tr>
<td>Accessing</td>
<td><code>hash<strong>[</strong>key<strong>]</strong></code></td>
<td>
<pre><code>hash = {:key => "value"}
hash<strong>[</strong>:key<strong>]</strong> # => "value"
hash<strong>[</strong>foo<strong>]</strong> # => nil
</code></pre>
</td>
<td>
Accessing an entry in a hash looks a lot like accessing it in
an <em>array</em>. However with a hash the key can be anything, not
just numbers. If you try to access a key that does not exist, the
value <code>nil</code> is returned, which is Ruby's way of saying
"nothing", because if it doesn't recognize the key it can't return
a value for it.
</td>
</tr>
<tr>
<td>Assigning</td>
<td>
<code>hash<strong>[</strong>key<strong>] =</strong> value</code>
</td>
<td>
<pre><code>hash = {:a => "b"}
hash<strong>[</strong>:key<strong>] =</strong> "value"
hash # => {:a=>"b", :key=>"value"}
</code></pre>
</td>
<td>
Assigning values to a hash is similar to assigning values to an
array. With a hash, the key can
be a number or it can be a symbol, string,
number... or anything, really!
</td>
</tr>
<tr>
<td>Deleting</td>
<td><code>hash<strong>.delete</strong><em>(key)</em></code></td>
<td>
<pre><code>hash = {:a => "b", :b => 10}
hash<strong>.delete</strong><em>(:a)</em>
hash # => {:b=>10}
</code></pre>
</td>
<td>
You can delete a specified key from the hash, so that the key and its
value can not be accessed.
</td>
</tr>
</table>
<h1 id="rails">Rails Basics</h1>
<p>This is an introduction to the basics of Rails. We look at the general
structure of a Rails application and the important commands used in the terminal.
</p>
<p>
If you do not have Rails installed yet, there is a
<a href="http://railsapps.github.io/installing-rails.html">
well maintained guide by Daniel Kehoe
</a> on how to install Rails on different platforms.
</p>
<h2 id="rails-folder-structure">The Structure of a Rails app</h2>
Here is an overview of all the folders of a new Rails application, outlining
the purpose of each folder, and describing the most important files.
<table class="table table-striped">
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<tr>
<td>app</td>
<td>This folder contains your application. Therefore it is the
<strong>most important folder</strong> in Ruby on Rails and it is
worth digging into its subfolders. See the following rows.
</td>
</tr>
<tr>
<td>app/assets</td>
<td>Assets basically are your front-end stuff. This folder contains
<em>images</em> you use on your website, <em>javascripts</em> for
all your fancy front-end interaction and <em>stylesheets</em> for
all your CSS making your website absolutely beautiful.
</td>
</tr>
<tr>
<td>app/controllers</td>
<td>The controllers subdirectory contains the controllers, which
handle the requests from
the users. It is often responsible for a single resource type,
such as places, users or attendees. Controllers also tie together
the <em>models</em> and the <em>views</em>.
</td>
</tr>
<tr>
<td>app/helpers</td>
<td>Helpers are used to take care of logic that is needed in the views
in order to keep the views clean of logic and reuse that logic in
multiple views.
</td>
</tr>
<tr>
<td>app/mailers</td>
<td>Functionality to send emails goes here.</td>
</tr>
<tr>
<td>app/models</td>
<td>The models subdirectory holds the classes that model the business
logic of our application. It is concerned with the things our
application is about. Often this is data, that is also saved in the
database. Examples here are a Person, or a Place class with all
their typical behaviour.
</td>
</tr>
<tr>
<td>app/views</td>
<td>
<p>The views subdirectory contains the display templates that will
be displayed to the user after a successful request.
By default they are written in HTML with embedded ruby
(.html.erb). The embedded ruby is used to insert data from the
application. It is then converted to HTML and sent to the browser
of the user. It has subdirectories for every resource of our
application, e.g. places, persons. These subdirectories contain the
associated view files.</p>
<p>Files starting with an underscore (<em>_</em>) are called
<em>partials</em>. Those are parts of a view which are reused in
other views. A common example is <em>_form.html.erb</em> which
contains the basic form for a given resource. It is used in the
<em>new</em> and in the <em>edit</em> view since creating
something
and editing something looks pretty similar.
</p>
</td>
</tr>
<tr>
<td>config</td>
<td>This directory contains the configuration files that your
application will need, including your database configuration (in
<em>database.yml</em>) and the particularly important
<em>routes.rb</em> which decides how web requests are handled.
The <em>routes.rb</em> file matches a given URL with the
<em>controller</em> which will handle the request.
</td>
</tr>
<tr>
<td>db</td>
<td>Contains a lot of <em>database</em> related files.
Most importantly the <em>migrations</em> subdirectory, containing
all your database migration files. Migrations set up your
database structure, including the attributes of your models. With migrations
you can add new attributes to existing models or create new models.
So you could add the <em>favorite_color</em> attribute to your
Person model so everyone can specify their favorite color.
</td>
</tr>
<tr>
<td>doc</td>
<td>Contains the documentation you create for your application. Not
too important when starting out.
</td>
</tr>
<tr>
<td>lib</td>
<td>Short for library. Contains code you've developed that is used in