This repository has been archived by the owner on Jan 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cheatsheet.html
580 lines (552 loc) · 25 KB
/
cheatsheet.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
<!DOCTYPE HTML>
<html>
<head lang="en-us">
<meta charset="UTF-8">
<title>Ruby Cheatsheet</title>
<link rel="stylesheet" type="text/css" href="stylesheets/cheatsheet_stylesheet.css">
<link href='http://fonts.googleapis.com/css?family=Josefin+Sans:300,400,600,300italic,400italic' rel='stylesheet' type='text/css'>
</head>
<body>
<nav>
<div id="title"> Ruby Cheatsheet</div>
</nav>
<div id="wrapper">
<div class= "topic">
<h2>General Resources</h2>
<table>
<tr>
<th>Website</th>
<th>Description</th>
</tr>
<tr>
<td><a href="www.stackoverflow.com">StackOverflow</a></td>
<td></td>
</tr>
<tr>
<td><a href="www.ruby-doc.org">Ruby-Docs</a></td>
<td></td>
</tr>
<tr>
<td><a href="www.typing.io">Typing Practice</a></td>
<td></td>
</tr>
<tr>
<td><a href="https://github.com/bbatsov/ruby-style-guide">Ruby Style Guide</a></td>
<td></td>
</tr>
</div>
<div class="topic">
<h2>Numbers</h2>
<p>
Numbers are what you would expect them to be, normal numbers that you use to perform basic math operations.
</p>
<table class="table table-striped">
<tbody>
<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>
</tbody>
</table>
</div>
<div class="topic">
<h2>Strings</h2>
<p>Strings are used to hold textual information. They may contain single characters, words, sentences or a whole book.</p>
<table class="table table-striped">
<tbody>
<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>
</tbody>
</table>
</div>
<div class="topic">
<h2>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.
<p>
<table class="table table-striped">
<tbody>
<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>
</tbody>
</table>
<!-- TABLE -->
<!-- GIST -->
</div>
<div class="topic">
<h2>Hashes</h2>
<p>Hashes associate a key to some value. You may then retrieve the value based upon its key. This construct is called a dictionary 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. 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. Symbols are especially common as keys. Symbols look like this: :symbol. 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>
<table class="table table-striped">
<tbody>
<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>
</tbody>
</table>
</div>
<div class = "topic">
<h2>Sublime Shortcuts</h2>
<table class="table table-striped">
<tbody><tr>
<th>Function</th>
<th class="shortcut">Shortcut</th>
<th>Description</th>
</tr>
<tr>
<td>Save file</td>
<td><strong>Ctrl + S</strong></td>
<td>
Saves the currently open file. If it was a new file you may also be
asked where to save it.
</td>
</tr>
<tr>
<td>Undo</td>
<td><strong>Ctrl + Z</strong></td>
<td>
Undo the last change you made to the current file. Can be applied
multiple times in succession to undo multiple changes.
</td>
</tr>
<tr>
<td>Redo</td>
<td><p><strong>Ctrl + Y</strong></p>
<p><em>or Ctrl + Shift + Z</em></p></td>
<td>
Redo what you just undid with <em>undo</em>, can also be done
multiple times.
</td>
</tr>
<tr>
<td>Find in File</td>
<td><strong>Ctrl + F</strong></td>
<td>
Search for a character sequence within the currently open file.
Hit <em>Enter</em> to progress to the next match.
</td>
</tr>
<tr>
<td>Find in all Files</td>
<td><strong>Ctrl + Shift + F</strong></td>
<td>Search for a character sequence in all files of the project.</td>
</tr>
<tr>
<td>Replace</td>
<td>
<p><strong>Ctrl + H</strong></p>
<p><em>or Ctrl + R</em></p>
</td>
<td>
Replace occurrences of the supplied character sequence with the
other supplied character sequence. Useful when renaming something.
</td>
</tr>
<tr>
<td>Copy</td>
<td><strong>Ctrl + C</strong></td>
<td>
Copy the currently highlighted text into the clipboard.
</td>
</tr>
<tr>
<td>Cut</td>
<td><strong>Ctrl + X</strong></td>
<td>Copy the highlighted text into the clipboard but delete it.</td>
</tr>
<tr>
<td>Paste</td>
<td><strong>Ctrl + V</strong></td>
<td>
Insert whatever currently is in the clipboard (through
<em>Copy</em> or <em>Cut</em>) at the current caret position.
Can insert multiple times.
</td>
</tr>
<tr>
<td>New File</td>
<td><strong>Ctrl + N</strong></td>
<td>Create a new empty file.</td>
</tr>
<tr>
<td>Search and open file</td>
<td><strong>Ctrl + P</strong></td>
<td>
Search for a file giving part of its name (<em>fuzzy search</em>).
Pressing <em>enter</em> will open the selected file.
</td>
</tr>
<tr>
<td>Comment</td>
<td><strong>Ctrl + /</strong></td>
<td>
Marks the selected text as a comment, which means that it will
be ignored. Useful when you want to see how something behaves or looks
without a specific section of code being run.
</td>
</tr>
</tbody></table>
</div>
</div>
</body>
</html>