-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslides_ruby_commandline.html
More file actions
907 lines (860 loc) · 56.7 KB
/
slides_ruby_commandline.html
File metadata and controls
907 lines (860 loc) · 56.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Slides for
Ruby — Ruby on Rails Guides
</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" data-turbo-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print">
<link rel="stylesheet" type="text/css" href="stylesheets/highlight.css" data-turbo-track="reload">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="stylesheets/reset.css">
<link rel="stylesheet" href="stylesheets/reveal.css">
<link rel="stylesheet" href="stylesheets/myslide.css" id="theme">
<link rel="stylesheet" href="stylesheets/code.css">
<script src="javascripts/clipboard.js" data-turbo-track="reload"></script>
<script src="javascripts/slides.js" data-turbo-track="reload"></script>
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>Ruby</h1><p>This guide will focus on Ruby - the language - alone.</p><p>After finishing this guide you will</p>
<ul>
<li>have an overview of Ruby's type system</li>
<li>be able to use list processing functions in Ruby</li>
<li>be able to use blocks and <code>yield</code> in Ruby.</li>
</ul>
<div class="interstitial repo"><p>Fork the <a href="https://github.com/backend-development/learn_ruby">learn_ruby</a> repository to try some test driven ruby learning</p></div>
<p><small>Slides - use arrow keys to navigate, esc to return to page view, f for fullscreen</small></p>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-0'>▻</a>
<h2 id="what-is-ruby"><a class="anchorlink" href="#what-is-ruby"><span>1</span> What is Ruby</a></h2><p><img src="images/Ruby_logo.png" alt="Ruby Logo"></p><p>Ruby is an open source project. It was started
in 1996 by Yukihiro 'Matz' Matsumoto. He is still
the "benevolent dictator" who decides on the future of the language.</p><p>In a colossal break with tradition he did not
choose a name starting with p for his scripting language
(think perl, python, php) but opted for r instead.</p></section>
<section><a class='slide_break' href='ruby_commandline.html#slide-1'>▻</a>
<p>Ruby is a thoroughly object oriented scripting language.
Even basic data types are object:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="mi">1</span><span class="p">.</span><span class="nf">to_s</span>
<span class="o">=></span> <span class="s2">"1"</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="1.to_s
=> "1"
">Copy</button>
</div>
<p>In this example the number 1 is used as an object, the method <code>to_s</code> is
called on it. The result is an Object of class String.</p></section>
<section><a class='slide_break' href='ruby_commandline.html#slide-2'>▻</a>
<h3 id="what-is-ruby-on-rails"><a class="anchorlink" href="#what-is-ruby-on-rails"><span>1.1</span> What is Ruby on Rails</a></h3><p><img src="images/Ruby_on_Rails_logo.png" alt="Ruby on Rails Logo"></p><p>Ruby on Rails is a web framework written in Ruby.
It was created by David Heinemeier Hansson ('DHH') starting in 2005.</p></section>
<section><a class='slide_break' href='ruby_commandline.html#slide-3'>▻</a>
<p>Rails is famous for the high productivity it gives to developers. It
is often used in startups, where speed of delivery is very important.
Rails moves fast, new versions with major improvement appear about every 18
months. The Rails community values speed of development, DRY code, testing,
version control, ... when you learn Rails you also pick up a whole culture
surrounding it.</p><p>Some sites built with Rails are: <a href="https://github.com">GitHub</a> and <a href="https://gitlab.com">GitLab</a>, <a href="https://shopify.com">Shopify</a>, <a href="https://www.fiverr.com/">Fiverr</a>, <a href="https://codepen.io/">codepen.io</a>, <a href="https://airbnb.com">Airbnb</a>, <a href="https://blog.twitch.tv/en/2015/12/18/twitch-engineering-an-introduction-and-overview-a23917b71a25/">Twitch</a>, <a href="https://squareup.com/">Square</a>, <a href="https://dribbble.com/">Dribble</a> and of course <a href="https://basecamp.com">Basecamp</a>, DHHs own product.</p><p>Read <a href="https://about.gitlab.com/blog/2018/10/29/why-we-use-rails-to-build-gitlab/">GitLab's article about why they use Rails</a></p></section>
<section><a class='slide_break' href='ruby_commandline.html#slide-4'>▻</a>
<h3 id="why-ruby-questionmark-why-rails-questionmark"><a class="anchorlink" href="#why-ruby-questionmark-why-rails-questionmark"><span>1.2</span> Why Ruby? Why Rails ?</a></h3><p>Why should you use Ruby and Rails over other programming languages
and frameworks?</p><p>Because of the cool t-shirts?</p><p><img src="images/ruby-and-rails-t-shirts.png" alt="Ruby and Rails T-Shirts"></p>
<ul>
<li>Because you want to be a highly productive web developer?</li>
<li>Because you want to be a well payed developer?</li>
</ul>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-5'>▻</a>
<p><a href="https://survey.stackoverflow.co/2024/work/#3-salary-and-experience-by-language">StackOverflows Survey from 2024</a> shows there are fewer Ruby developers, but they are better payed:</p><p><img src="images/salary-2024.png" alt=""></p></section>
<section><a class='slide_break' href='ruby_commandline.html#slide-6'>▻</a>
<h2 id="ruby-basics"><a class="anchorlink" href="#ruby-basics"><span>2</span> Ruby Basics</a></h2><p>To get to know Ruby you don't need to write
whole programes. You can start out with <strong>interactive ruby - irb</strong>.
When you start irb you get a command line to type in Ruby code. When
you press enter the code is evaluated immediately and you get the result.
Use the commands <code>exit</code> or <code>quit</code> or the key combination CONTROL-D to get out.</p><p>In this guide we will show code run in irb by marking the prompt as <code>>></code> and
the result as <code>=></code>, for example:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="o">>></span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span>
<span class="o">=></span> <span class="mi">4</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text=">> 2 + 2
=> 4
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-7'>▻</a>
<h3 id="some-code-conventions"><a class="anchorlink" href="#some-code-conventions"><span>2.1</span> Some code conventions</a></h3><p>Try to stick to <a href="https://rubystyle.guide/">github's style for ruby</a>.</p><p>When you choose names for your objects, classes and methods
you should stick to the following conventions to avoid
confusing other Ruby developers:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="n">the_variable</span> <span class="o">=</span> <span class="no">SomeClass</span><span class="p">.</span><span class="nf">new</span>
<span class="c1"># variables are written in snake_case</span>
<span class="c1"># classes in capital CamelCase</span>
<span class="c1"># method names are written in snake_case</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">b</span><span class="p">.</span><span class="nf">sugar</span> <span class="c1"># a method that returns something</span>
<span class="n">b</span><span class="p">.</span><span class="nf">sweet?</span> <span class="c1"># a method that returns true or false</span>
<span class="c1"># ends in a question mark</span>
<span class="n">b</span><span class="p">.</span><span class="nf">sugar!</span> <span class="c1"># a method that changes its object</span>
<span class="c1"># ends in an exclamation mark</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="the_variable = SomeClass.new
# variables are written in snake_case
# classes in capital CamelCase
# method names are written in snake_case
a = b.sugar # a method that returns something
b.sweet? # a method that returns true or false
# ends in a question mark
b.sugar! # a method that changes its object
# ends in an exclamation mark
">Copy</button>
</div>
<p>In the last two examples the punctuation marks are really
part of the method names!</p></section>
<section><a class='slide_break' href='ruby_commandline.html#slide-8'>▻</a>
<h2 id="methods"><a class="anchorlink" href="#methods"><span>3</span> Methods</a></h2><p>When calling a method,
the parantheses around the arguments are optional.
Leave them off unless your code get's confusing:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="nb">puts</span><span class="p">(</span><span class="s2">"less code"</span><span class="p">)</span>
<span class="nb">puts</span> <span class="s2">"less code"</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="puts("less code")
puts "less code"
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-9'>▻</a>
<h3 id="definig-a-method"><a class="anchorlink" href="#definig-a-method"><span>3.1</span> Definig a Method</a></h3><div class="interstitial code">
<pre><code class="highlight ruby"><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">)</span>
<span class="nb">puts</span> <span class="s2">"Hello World"</span>
<span class="nb">puts</span> <span class="s2">"I'm just ignoring my arguments for now"</span>
<span class="k">end</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="def f(a,b)
puts "Hello World"
puts "I'm just ignoring my arguments for now"
end
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-10'>▻</a>
<h3 id="return-value"><a class="anchorlink" href="#return-value"><span>3.2</span> Return Value</a></h3><p>Methods in Ruby return the last expression - even
if no explicit <code>return</code> statement is given.</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">)</span>
<span class="s2">"x"</span>
<span class="k">end</span>
<span class="n">f</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">42</span><span class="p">)</span> <span class="c1"># returns "x"</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="def f(a,b)
"x"
end
f(1,42) # returns "x"
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-11'>▻</a>
<h3 id="keyword-arguments"><a class="anchorlink" href="#keyword-arguments"><span>3.3</span> Keyword Arguments</a></h3><p>Since ruby 2.0 keyword arguments can be used,
and can be given default values:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="k">def</span> <span class="nf">apply_the_style</span><span class="p">(</span><span class="ss">font_family: </span><span class="s1">'MS Comic Sans'</span><span class="p">,</span> <span class="ss">font_size: </span><span class="mi">16</span> <span class="p">)</span>
<span class="o">...</span>
<span class="k">end</span>
<span class="n">apply_the_style</span><span class="p">()</span> <span class="c1"># uses both default values</span>
<span class="n">apply_the_style</span><span class="p">(</span><span class="ss">font_family: </span><span class="s2">"Arial"</span><span class="p">)</span>
<span class="n">apply_the_style</span><span class="p">(</span><span class="ss">font_size: </span><span class="mi">10</span><span class="p">,</span> <span class="ss">font_family: </span><span class="s2">"Arial"</span><span class="p">)</span> <span class="c1"># order can be different</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="def apply_the_style(font_family: 'MS Comic Sans', font_size: 16 )
...
end
apply_the_style() # uses both default values
apply_the_style(font_family: "Arial")
apply_the_style(font_size: 10, font_family: "Arial") # order can be different
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-12'>▻</a>
<h2 id="object-orientation"><a class="anchorlink" href="#object-orientation"><span>4</span> Object Orientation</a></h2><p>Everything is an Object, even Integers and Strings.
They have methods and properties, like other objects:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="o">>></span> <span class="kp">nil</span><span class="p">.</span><span class="nf">class</span>
<span class="o">=></span> <span class="no">NilClass</span>
<span class="o">>></span> <span class="mi">2</span><span class="p">.</span><span class="nf">class</span>
<span class="o">=></span> <span class="no">Fixnum</span>
<span class="o">>></span> <span class="s2">"some text"</span><span class="p">.</span><span class="nf">length</span>
<span class="o">=></span> <span class="mi">9</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text=">> nil.class
=> NilClass
>> 2.class
=> Fixnum
>> "some text".length
=> 9
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-13'>▻</a>
<h3 id="the-save-navigation-operator"><a class="anchorlink" href="#the-save-navigation-operator"><span>4.1</span> The 'Save Navigation' Operator</a></h3><p>Imagine you have an Object <code>a</code>, that has a property <code>b</code> containing
another object, and <code>b</code> has a property <code>c</code>. You can access <code>c</code> through <code>a.b.c</code></p><p>But if <code>a</code> is <code>nil</code> then you will get an error:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="o">></span> <span class="n">a</span><span class="p">.</span><span class="nf">b</span><span class="p">.</span><span class="nf">c</span>
<span class="no">NoMethodError</span><span class="p">:</span> <span class="n">undefined</span> <span class="nb">method</span> <span class="sb">`b' for nil:NilClass
</span></code></pre>
<button class="clipboard-button" data-clipboard-text="> a.b.c
NoMethodError: undefined method `b' for nil:NilClass
">Copy</button>
</div>
<p>The Operator <code>&.</code> avoids this error. It is called "save navigation operator" or sometimes "lonely operator".</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="o">></span> <span class="n">a</span><span class="o">&</span><span class="p">.</span><span class="nf">b</span><span class="o">&</span><span class="p">.</span><span class="nf">c</span>
<span class="kp">nil</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="> a&.b&.c
nil
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-14'>▻</a>
<h2 id="type-system"><a class="anchorlink" href="#type-system"><span>5</span> Type System</a></h2><p>All of Ruby's basic data types are Classes.</p>
<ul>
<li>nil <code># NilClass</code></li>
<li>Numeric, Integer, Fixnum, Bignum, Float <code># are converted automatically to each other</code></li>
<li>Ranges</li>
<li>String</li>
<li>true <code># TrueClass</code></li>
<li>false <code># FalseClass</code></li>
<li>Symbol</li>
<li>Array</li>
<li>Hash</li>
<li>Object</li>
<li>Regex</li>
</ul>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-15'>▻</a>
<h3 id="strict-type-checking"><a class="anchorlink" href="#strict-type-checking"><span>5.1</span> Strict Type Checking</a></h3><p>Ruby is strict about data types, there is no automatic conversion except
between numeric types.</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="o">>></span> <span class="s2">"a string"</span> <span class="o">+</span> <span class="s2">"another"</span>
<span class="o">=></span> <span class="s2">"a stringanother"</span>
<span class="o">>></span> <span class="s2">"a string"</span> <span class="o">+</span> <span class="mi">2</span>
<span class="no">TypeError</span><span class="p">:</span> <span class="n">no</span> <span class="n">implicit</span> <span class="n">conversion</span> <span class="n">of</span> <span class="no">Fixnum</span> <span class="n">into</span> <span class="no">String</span>
<span class="o">>></span> <span class="mi">42</span> <span class="o">+</span> <span class="mf">3.141</span>
<span class="o">=></span> <span class="mf">45.141</span>
<span class="o">>></span> <span class="s2">":-)"</span> <span class="o">*</span> <span class="mi">4</span>
<span class="o">=></span> <span class="s2">":-):-):-):-)"</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text=">> "a string" + "another"
=> "a stringanother"
>> "a string" + 2
TypeError: no implicit conversion of Fixnum into String
>> 42 + 3.141
=> 45.141
>> ":-)" * 4
=> ":-):-):-):-)"
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-16'>▻</a>
<h3 id="strings"><a class="anchorlink" href="#strings"><span>5.2</span> Strings</a></h3><div class="interstitial code">
<pre><code class="highlight ruby"><span class="n">s</span> <span class="o">=</span> <span class="s1">'just a string of characters'</span>
<span class="n">s</span> <span class="o">=</span> <span class="s2">"string with </span><span class="si">#{</span><span class="n">the_variable</span><span class="si">}</span><span class="s2"> embedded"</span>
<span class="n">s</span> <span class="o">=</span> <span class="s2">"string with </span><span class="si">#{</span><span class="n">a</span><span class="o">+</span><span class="n">b</span><span class="o">/</span><span class="n">c</span><span class="si">}</span><span class="s2"> a ruby expression embedded"</span>
<span class="n">s</span> <span class="o">=</span> <span class="o"><<</span><span class="no">EOM</span><span class="sh">
This is a so called "Here-Document"
it can contain many lines of text
and ends with the identifier EOM (that i chose!)
but only if it's alone on a line all by itself:
</span><span class="no">EOM</span>
<span class="n">s</span> <span class="o">=</span> <span class="sx">%Q|with %Q you can chose any character als string delimiters|</span>
<span class="n">s</span> <span class="o">=</span> <span class="sx">%Q{
with %Q you can chose any character als string delimiters.
opening brackets go with closing brackets.
}</span>
<span class="n">s</span> <span class="o">=</span> <span class="s1">'aBC'</span>
<span class="nb">puts</span> <span class="n">s</span><span class="p">.</span><span class="nf">upcase</span> <span class="c1"># ABC</span>
<span class="nb">puts</span> <span class="n">s</span><span class="p">.</span><span class="nf">downcase</span> <span class="c1"># abc</span>
<span class="nb">puts</span> <span class="n">s</span><span class="p">.</span><span class="nf">capitalize</span> <span class="c1"># Abc</span>
<span class="nb">puts</span> <span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="c1"># a (index of character)</span>
<span class="nb">puts</span> <span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">2</span><span class="p">]</span> <span class="c1"># aB (start + length)</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="s = 'just a string of characters'
s = "string with #{the_variable} embedded"
s = "string with #{a+b/c} a ruby expression embedded"
s = <<EOM
This is a so called "Here-Document"
it can contain many lines of text
and ends with the identifier EOM (that i chose!)
but only if it's alone on a line all by itself:
EOM
s = %Q|with %Q you can chose any character als string delimiters|
s = %Q{
with %Q you can chose any character als string delimiters.
opening brackets go with closing brackets.
}
s = 'aBC'
puts s.upcase # ABC
puts s.downcase # abc
puts s.capitalize # Abc
puts s[0] # a (index of character)
puts s[0,2] # aB (start + length)
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-17'>▻</a>
<h3 id="boolean-values"><a class="anchorlink" href="#boolean-values"><span>5.3</span> Boolean Values</a></h3><p>In Ruby only <code>false</code> and <code>nil</code> are treated as false. This might
be confusing for programmers used to other languages with
more complex rules for truthyness:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="k">if</span> <span class="mi">0</span>
<span class="nb">puts</span> <span class="s1">'0 is true!'</span>
<span class="k">end</span>
<span class="k">if</span> <span class="s2">"false"</span>
<span class="nb">puts</span> <span class="s1">'"false" (the string) is true'</span>
<span class="k">end</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="if 0
puts '0 is true!'
end
if "false"
puts '"false" (the string) is true'
end
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-18'>▻</a>
<h3 id="symbols"><a class="anchorlink" href="#symbols"><span>5.4</span> Symbols</a></h3><p>A Symbol looks - at first glance - similar to a string: you can
invent it at any time (no 'declaration') and give it any name:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="n">a</span> <span class="o">=</span> <span class="ss">:foo</span>
<span class="n">a</span> <span class="o">=</span> <span class="ss">:bar</span>
<span class="n">s</span> <span class="o">=</span> <span class="s2">"foo"</span>
<span class="n">s</span> <span class="o">=</span> <span class="s2">"bar"</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="a = :foo
a = :bar
s = "foo"
s = "bar"
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-19'>▻</a>
<p>But: there is always just one instance of a symbol while
there can be several strings that have the same content, but are different objects:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="o">>></span> <span class="ss">:foo</span><span class="p">.</span><span class="nf">object_id</span>
<span class="o">=></span> <span class="mi">635528</span>
<span class="o">>></span> <span class="ss">:foo</span><span class="p">.</span><span class="nf">object_id</span>
<span class="o">=></span> <span class="mi">635528</span>
<span class="o">>></span> <span class="s2">"foo"</span><span class="p">.</span><span class="nf">object_id</span>
<span class="o">=></span> <span class="mi">70099463087600</span>
<span class="o">>></span> <span class="s2">"foo"</span><span class="p">.</span><span class="nf">object_id</span>
<span class="o">=></span> <span class="mi">70099463106400</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text=">> :foo.object_id
=> 635528
>> :foo.object_id
=> 635528
>> "foo".object_id
=> 70099463087600
>> "foo".object_id
=> 70099463106400
">Copy</button>
</div>
<p>Use symbols where you would enums in a database or another language,
or if you need distinct constants, when the value is not important.</p></section>
<section><a class='slide_break' href='ruby_commandline.html#slide-20'>▻</a>
<h3 id="arrays"><a class="anchorlink" href="#arrays"><span>5.5</span> Arrays</a></h3><p>There are several ways of writing literal arrays in Ruby.
The first one looks like JSON:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="o">>></span> <span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"this"</span><span class="p">,</span> <span class="s2">"that"</span><span class="p">,</span> <span class="s2">"something"</span><span class="p">]</span>
<span class="o">=></span> <span class="p">[</span><span class="s2">"this"</span><span class="p">,</span> <span class="s2">"that"</span><span class="p">,</span> <span class="s2">"something"</span><span class="p">]</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text=">> a = ["this", "that", "something"]
=> ["this", "that", "something"]
">Copy</button>
</div>
<p>For creating an array of words (strings without whitespace in them)
you can use <code>%w</code>:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="o">>></span> <span class="n">a</span> <span class="o">=</span> <span class="sx">%w(this that something)</span>
<span class="o">=></span> <span class="p">[</span><span class="s2">"this"</span><span class="p">,</span> <span class="s2">"that"</span><span class="p">,</span> <span class="s2">"something"</span><span class="p">]</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text=">> a = %w(this that something)
=> ["this", "that", "something"]
">Copy</button>
</div>
<p>When creating an array of consecutive numbers you can
use a Range and convert it to an Array:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="o">>></span> <span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="mi">4</span><span class="p">).</span><span class="nf">to_a</span>
<span class="o">=></span> <span class="p">[</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>
</code></pre>
<button class="clipboard-button" data-clipboard-text=">> (1..4).to_a
=> [1, 2, 3, 4]
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-21'>▻</a>
<h3 id="hashes"><a class="anchorlink" href="#hashes"><span>5.6</span> Hashes</a></h3><p>A Hash is a datastructure similar to an array. An array uses integers as keys
while a Hash allows any type as the keys. Mostly strings and symbols are used:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="n">h</span> <span class="o">=</span> <span class="no">Hash</span><span class="p">.</span><span class="nf">new</span>
<span class="n">h</span><span class="p">[</span><span class="s2">"alice"</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"beer"</span>
<span class="n">h</span><span class="p">[</span><span class="s2">"chris"</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"tea"</span>
<span class="n">h</span><span class="p">[</span><span class="s2">"bob"</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"mate"</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="h = Hash.new
h["alice"] = "beer"
h["chris"] = "tea"
h["bob"] = "mate"
">Copy</button>
</div>
<p>But you can use other objects:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="n">t</span> <span class="o">=</span> <span class="no">Date</span><span class="p">.</span><span class="nf">new</span>
<span class="n">h</span><span class="p">[</span><span class="n">t</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"recently"</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="t = Date.new
h[t] = "recently"
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-22'>▻</a>
<h4 id="hash-implementation"><a class="anchorlink" href="#hash-implementation"><span>5.6.1</span> Hash implementation</a></h4><p>The data structure behind a Ruby Hash is more complex
than an array: The key is sent through a function (called hash function)
that returns a number. This number is used as the index
for an array. If the result for two keys is the
same, a linked list is built.</p><p><img src="images/hash_table.svg" alt="How Hash(tabl)es work"></p></section>
<section><a class='slide_break' href='ruby_commandline.html#slide-23'>▻</a>
<h4 id="why-this-hash-implementation-questionmark"><a class="anchorlink" href="#why-this-hash-implementation-questionmark"><span>5.6.2</span> Why this Hash implementation?</a></h4><p>This datastructure seems like a serious waste of memory
at first. But it offers the following interesting features:</p>
<ul>
<li>looking up a key can be accomplished in constant time</li>
<li>inserting a new key / value pair can be accomplished in constant time</li>
</ul>
<p>Most scripting languages offer Hashes as a basic data type,
most compiled languages as a library. Read more about
Hashes in Wikipedia:</p>
<ul>
<li><a href="https://en.wikipedia.org/wiki/Hash_table">Hashtables in Wikipedia</a></li>
</ul>
<p>(If you don't know what "in constant time" means above, you
should learn more about the analysis of algorithms. e.g. by
taking an algorithms and data structure course as offered in the second
semester of most computer science programs.)</p></section>
<section><a class='slide_break' href='ruby_commandline.html#slide-24'>▻</a>
<h4 id="implicit-form"><a class="anchorlink" href="#implicit-form"><span>5.6.3</span> Implicit Form</a></h4><p>A Hash can be created with Hash.new, or by writing it as a literal:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="n">roomnumber</span> <span class="o">=</span> <span class="p">{</span> <span class="s2">"Jane Doe"</span> <span class="o">=></span> <span class="mi">10</span><span class="p">,</span> <span class="s2">"Jim Doe"</span> <span class="o">=></span> <span class="mi">6</span> <span class="p">}</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="roomnumber = { "Jane Doe" => 10, "Jim Doe" => 6 }
">Copy</button>
</div>
<p>When the keys are symbols you can use an alternative syntax that
looks like json</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="n">style</span> <span class="o">=</span> <span class="p">{</span> <span class="ss">:font_size</span> <span class="o">=></span> <span class="mi">10</span><span class="p">,</span> <span class="ss">:font_family</span> <span class="o">=></span> <span class="s2">"Arial"</span> <span class="p">}</span>
<span class="n">style</span> <span class="o">=</span> <span class="p">{</span> <span class="ss">font_size: </span><span class="mi">10</span><span class="p">,</span> <span class="ss">font_family: </span><span class="s2">"Arial"</span> <span class="p">}</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="style = { :font_size => 10, :font_family => "Arial" }
style = { font_size: 10, font_family: "Arial" }
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-25'>▻</a>
<h4 id="a-hash-as-an-argument"><a class="anchorlink" href="#a-hash-as-an-argument"><span>5.6.4</span> A Hash as an Argument</a></h4><p>Before keyword arguments were added to ruby,
often a hash was used as the single argument for a method.
Calling the method then reads like named arguments:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="k">def</span> <span class="nf">apply_the_style</span><span class="p">(</span> <span class="n">h</span> <span class="p">)</span>
<span class="o">...</span>
<span class="k">end</span>
<span class="n">apply_the_style</span><span class="p">(</span><span class="ss">font_size: </span><span class="mi">10</span><span class="p">,</span> <span class="ss">font_family: </span><span class="s2">"Arial"</span><span class="p">)</span>
<span class="n">apply_the_style</span> <span class="ss">font_size: </span><span class="mi">10</span><span class="p">,</span> <span class="ss">font_family: </span><span class="s2">"Arial"</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="def apply_the_style( h )
...
end
apply_the_style(font_size: 10, font_family: "Arial")
apply_the_style font_size: 10, font_family: "Arial"
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-26'>▻</a>
<h2 id="frozen-vs-mutable"><a class="anchorlink" href="#frozen-vs-mutable"><span>6</span> Frozen vs Mutable</a></h2><p>Most objects in Ruby are mutabel by default.</p><div class="interstitial code">
<pre><code class="highlight plaintext"># mutable
>> configuration = { color: 'yellow', number: 10 }
=> {:color=>"yellow", :number=>10}
>> configuration[:smoking] = false # no problem, can add new key
=> false
>> configuration[:number] = 2 # no problem, change value
=> 2
>> configuration
=> {:color=>"yellow", :number=>2, :smoking=>false}
</code></pre>
<button class="clipboard-button" data-clipboard-text="# mutable
>> configuration = { color: 'yellow', number: 10 }
=> {:color=>"yellow", :number=>10}
>> configuration[:smoking] = false # no problem, can add new key
=> false
>> configuration[:number] = 2 # no problem, change value
=> 2
>> configuration
=> {:color=>"yellow", :number=>2, :smoking=>false}
">Copy</button>
</div>
<p>You can freeze an object to make it immutable.
Notice that this is different from using a <code>const</code>
in JavaScript: changing the object in any way
will throw a runtime error.</p><div class="interstitial code">
<pre><code class="highlight plaintext"># frozen
>> configuration = { color: 'yellow', number: 10 }.freeze
=> {:color=>"yellow", :number=>10}
>> configuration[:smoking] = false
=> RuntimeError: can't modify frozen Hash
>> configuration[:number] = 2
=>RuntimeError: can't modify frozen Hash
</code></pre>
<button class="clipboard-button" data-clipboard-text="# frozen
>> configuration = { color: 'yellow', number: 10 }.freeze
=> {:color=>"yellow", :number=>10}
>> configuration[:smoking] = false
=> RuntimeError: can't modify frozen Hash
>> configuration[:number] = 2
=>RuntimeError: can't modify frozen Hash
">Copy</button>
</div>
<p>You cannot modify a frozen object, but you can copy it with <code>.dup</code></p></section>
<section><a class='slide_break' href='ruby_commandline.html#slide-27'>▻</a>
<h3 id="frozen-string-literals"><a class="anchorlink" href="#frozen-string-literals"><span>6.1</span> Frozen String Literals</a></h3><p>Since Ruby 2.3 all String literals can be frozen by adding
this line to to top of a ruby file:</p><div class="interstitial code">
<pre><code class="highlight plaintext"># frozen_string_literal: true
</code></pre>
<button class="clipboard-button" data-clipboard-text="# frozen_string_literal: true
">Copy</button>
</div>
<p>This is switched on in Rails since 5.2.</p></section>
<section><a class='slide_break' href='ruby_commandline.html#slide-28'>▻</a>
<h2 id="conditions"><a class="anchorlink" href="#conditions"><span>7</span> Conditions</a></h2><p>The Basic condition with <code>if</code> works like in most programming languages.</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="k">if</span> <span class="n">i</span> <span class="o">></span> <span class="mi">10</span>
<span class="nb">puts</span> <span class="s1">'cannot compute, not enough fingers'</span>
<span class="k">elsif</span> <span class="n">i</span> <span class="o"><=</span> <span class="mi">0</span>
<span class="nb">puts</span> <span class="s1">'cannot compute, negative number'</span>
<span class="k">else</span>
<span class="nb">puts</span> <span class="s1">'input correct'</span>
<span class="k">end</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="if i > 10
puts 'cannot compute, not enough fingers'
elsif i <= 0
puts 'cannot compute, negative number'
else
puts 'input correct'
end
">Copy</button>
</div>
<p>The <code>case</code> expression can match one value in several ways.</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="k">case</span> <span class="n">x</span>
<span class="k">when</span> <span class="mi">1</span><span class="o">..</span><span class="mi">5</span>
<span class="nb">puts</span> <span class="s2">"It's between 1 and 5"</span>
<span class="k">when</span> <span class="mi">6</span>
<span class="nb">puts</span> <span class="s2">"It's 6"</span>
<span class="k">when</span> <span class="s2">"foo"</span><span class="p">,</span> <span class="s2">"bar"</span>
<span class="nb">puts</span> <span class="s2">"It's either foo or bar"</span>
<span class="k">when</span> <span class="no">String</span>
<span class="nb">puts</span> <span class="s2">"You passed a string"</span>
<span class="k">else</span>
<span class="nb">puts</span> <span class="s2">"You gave me </span><span class="si">#{</span><span class="n">x</span><span class="si">}</span><span class="s2"> -- I have no idea what to do with that."</span>
<span class="k">end</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="case x
when 1..5
puts "It's between 1 and 5"
when 6
puts "It's 6"
when "foo", "bar"
puts "It's either foo or bar"
when String
puts "You passed a string"
else
puts "You gave me #{x} -- I have no idea what to do with that."
end
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-29'>▻</a>
<h3 id="shorthand-version"><a class="anchorlink" href="#shorthand-version"><span>7.1</span> Shorthand version</a></h3><p>If the <code>if</code> only has one statement
you can write it in a shorthand version:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="k">if</span> <span class="n">errors</span> <span class="o">></span> <span class="mi">0</span>
<span class="nb">puts</span> <span class="s2">"Some errors occured"</span>
<span class="k">end</span>
<span class="nb">puts</span> <span class="s2">"Some errors occured"</span> <span class="k">if</span> <span class="n">errors</span> <span class="o">></span> <span class="mi">0</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="if errors > 0
puts "Some errors occured"
end
puts "Some errors occured" if errors > 0
">Copy</button>
</div>
<p>This syntax should be familiar to you if
you understand English. (yes, that's an English
sentence using the same syntax).</p></section>
<section><a class='slide_break' href='ruby_commandline.html#slide-30'>▻</a>
<h3 id="boolean-operators-as-conditions"><a class="anchorlink" href="#boolean-operators-as-conditions"><span>7.2</span> Boolean Operators as conditions</a></h3><p>When Ruby evaluates a boolean operator,
it does as little work as possible. It
stops evaluation as soon as the result is clear:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="c1"># the second argument is not evaluated!</span>
<span class="n">a</span> <span class="o">=</span> <span class="kp">true</span> <span class="o">||</span> <span class="o">...</span>
<span class="n">a</span> <span class="o">=</span> <span class="kp">false</span> <span class="o">&&</span> <span class="o">...</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="# the second argument is not evaluated!
a = true || ...
a = false && ...
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-31'>▻</a>
<p>The boolean operators don't just return true or
false, they return the argument last evaluated.
This is often used to set a variable:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="n">default_value</span> <span class="o">=</span> <span class="s2">"gray"</span>
<span class="n">input_value</span> <span class="o">=</span> <span class="kp">nil</span>
<span class="c1"># here input_value might be set...</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">input_value</span> <span class="o">||</span> <span class="n">default_value</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="default_value = "gray"
input_value = nil
# here input_value might be set...
a = input_value || default_value
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-32'>▻</a>
<h2 id="enumerables"><a class="anchorlink" href="#enumerables"><span>8</span> Enumerables</a></h2><p>When working with a list of values Ruby
helps you think about data on a new, more abstract level
with Enumerables:</p></section>
<section><a class='slide_break' href='ruby_commandline.html#slide-33'>▻</a>
<h3 id="piping-data"><a class="anchorlink" href="#piping-data"><span>8.1</span> Piping Data</a></h3><p>From the UNIX shell you may know the concept of piping data
from one command to the next:</p><div class="interstitial code">
<pre><code class="highlight shell"><span class="c"># I know mysql server is running, but which user is it running as ?</span>
<span class="c">#</span>
<span class="nv">$ </span>ps aux | <span class="nb">grep </span>mysqld_safe | <span class="nb">head</span> <span class="nt">-1</span> | <span class="nb">cut</span> <span class="nt">-c1-8</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="# I know mysql server is running, but which user is it running as ?
#
$ ps aux | grep mysqld_safe | head -1 | cut -c1-8
">Copy</button>
</div>
<p>This is a sequence of Unix programs connected by the pipe symbol (|).
Each program in this chain reads data from its "Standard Input" and
writes data to its "Standard Output." The pipe symbol serves as a conduit,
channeling the output from one program as input to the subsequent program in the sequence.
The data being transferred is plain text, often spanning multiple lines.</p><p>Try it out on your commmand line by building up
the pipe step by step:</p><div class="interstitial code">
<pre><code class="highlight shell"><span class="nv">$ </span>ps aux | less
<span class="nv">$ </span>ps aux | <span class="nb">grep </span>mysqld_safe | less
<span class="nv">$ </span>ps aux | <span class="nb">grep </span>mysqld_safe | <span class="nb">head</span> <span class="nt">-1</span> <span class="o">||</span> less
<span class="nv">$ </span>ps aux | <span class="nb">grep </span>mysqld_safe | <span class="nb">head</span> <span class="nt">-1</span> | <span class="nb">cut</span> <span class="nt">-c1-8</span> | less
</code></pre>
<button class="clipboard-button" data-clipboard-text="$ ps aux | less
$ ps aux | grep mysqld_safe | less
$ ps aux | grep mysqld_safe | head -1 || less
$ ps aux | grep mysqld_safe | head -1 | cut -c1-8 | less
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-34'>▻</a>
<h3 id="piping-data-in-ruby"><a class="anchorlink" href="#piping-data-in-ruby"><span>8.2</span> Piping Data in Ruby</a></h3><p>When piping data in ruby you can start with
an Array</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="n">languages</span> <span class="o">=</span> <span class="sx">%w[Fortran Ada C C++ Java Scala Haskell]</span>
<span class="n">languages</span><span class="p">.</span><span class="nf">sort</span><span class="p">.</span><span class="nf">first</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="languages = %w[Fortran Ada C C++ Java Scala Haskell]
languages.sort.first(3)
">Copy</button>
</div>
<p>The elements of the array are piped into the sort-method, which again outputs a list
of elements. These are piped into first, which only returns the first three and discards
the rest. The result is a list of 3 elements.</p></section>
<section><a class='slide_break' href='ruby_commandline.html#slide-35'>▻</a>
<p>Here are some simple methods you can use on Arrays (and other Enumerables)
that return a new Enumerable. You can connect theses methods to each other:</p>
<ul>
<li>sort</li>
<li>first(n)</li>
<li>drop(n)</li>
<li>last(n)</li>
<li>grep(/pattern/)</li>
<li>chunk(method)</li>
<li>reverse</li>
</ul>
<p>The method <code>tally</code> counts the occourance of elements and returns a hash:</p><div class="interstitial code">
<pre><code class="highlight plaintext">["a", "b", "c", "b"].tally
#=> {"a"=>1, "b"=>2, "c"=>1}
</code></pre>
<button class="clipboard-button" data-clipboard-text="["a", "b", "c", "b"].tally
#=> {"a"=>1, "b"=>2, "c"=>1}
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-36'>▻</a>
<p>Some other methods return just a single value, and thus end the pipe:</p>
<ul>
<li>count</li>
<li>count("only this exact value")</li>
<li>max</li>
<li>min</li>
</ul>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-37'>▻</a>
<p>More advanced methods take a Block (of code) as their argument.
The method <code>map</code> applys the Block to each piece of data, and
returns an Enumerable of the new data:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="o">>></span> <span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="mi">10</span><span class="p">).</span><span class="nf">map</span><span class="p">{</span> <span class="o">|</span><span class="n">x</span><span class="o">|</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="p">}</span>
<span class="o">=></span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">18</span><span class="p">,</span> <span class="mi">20</span><span class="p">]</span>
<span class="o">>></span> <span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="mi">10</span><span class="p">).</span><span class="nf">map</span><span class="p">{</span> <span class="o">|</span><span class="n">x</span><span class="o">|</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="p">}.</span><span class="nf">reverse</span>
<span class="o">=></span> <span class="p">[</span><span class="mi">20</span><span class="p">,</span> <span class="mi">18</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text=">> (1..10).map{ |x| 2*x }
=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
>> (1..10).map{ |x| 2*x }.reverse
=> [20, 18, 16, 14, 12, 10, 8, 6, 4, 2]
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-38'>▻</a>
<p>If the computation is more complex you can write
the Block on several lines, ending with <code>end</code></p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="o">>></span> <span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="mi">10</span><span class="p">).</span><span class="nf">map</span> <span class="k">do</span> <span class="o">|</span><span class="n">x</span><span class="o">|</span>
<span class="sc">?></span> <span class="n">x</span><span class="o">*</span><span class="mi">2</span>
<span class="o">>></span> <span class="k">end</span>
<span class="o">=></span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">18</span><span class="p">,</span> <span class="mi">20</span><span class="p">]</span>
<span class="o">>></span> <span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="mi">10</span><span class="p">).</span><span class="nf">map</span> <span class="k">do</span> <span class="o">|</span><span class="n">x</span><span class="o">|</span>
<span class="sc">?></span> <span class="n">x</span><span class="o">*</span><span class="mi">2</span>
<span class="o">>></span> <span class="k">end</span><span class="p">.</span><span class="nf">reverse</span>
<span class="o">=></span> <span class="p">[</span><span class="mi">20</span><span class="p">,</span> <span class="mi">18</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text=">> (1..10).map do |x|
?> x*2
>> end
=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
>> (1..10).map do |x|
?> x*2
>> end.reverse
=> [20, 18, 16, 14, 12, 10, 8, 6, 4, 2]
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-39'>▻</a>
<p>Some other methods for Enumerables that take a Block:</p>
<ul>
<li><code>map {|x|</code> new value computed from x <code>}</code></li>
<li><code>map.with_index {|x, i|</code> new value computed from x and i, the position in the array <code>}</code></li>
<li><code>select {|x|</code> should x be selected? <code>}</code></li>
<li><code>reduce(:+)</code></li>
<li><code>reduce{|memo, item|</code> compute new value for memo, using current item <code>}</code></li>
</ul>
<p>These methods should help you avoid loops and thus simplify
code considerably.</p><p>Read the Reference on</p>
<ul>
<li><a href="https://ruby-doc.org/core-2.5.3/Enumerable.html">Enumerable</a></li>
<li><a href="https://ruby-doc.org/core-2.5.3/Array.html">Array</a></li>
</ul>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-40'>▻</a>
<h2 id="blocks"><a class="anchorlink" href="#blocks"><span>9</span> Blocks</a></h2><p>Blocks of code are not just used in Enumerables, they are
a basic building block of ruby. You can write functions
that take a Block as an argument:</p></section>
<section><a class='slide_break' href='ruby_commandline.html#slide-41'>▻</a>
<h3 id="my-function-takes-a-block-of-code"><a class="anchorlink" href="#my-function-takes-a-block-of-code"><span>9.1</span> My Function takes a Block of Code</a></h3><p>Any function you write can take an additional block
of code as its last argument. The block is only
called if and when you call <code>yield</code> inside the function:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="k">def</span> <span class="nf">my_function_with_block_arg</span>
<span class="nb">puts</span> <span class="s2">"code in the funtion"</span>
<span class="k">yield</span>
<span class="nb">puts</span> <span class="s2">"more code in the function"</span>
<span class="k">end</span>
<span class="n">my_function_with_block_arg</span> <span class="p">{</span> <span class="nb">puts</span> <span class="s2">"code in the block"</span> <span class="p">}</span>
<span class="c1"># OUTPUT:</span>
<span class="c1"># code in the funtion</span>
<span class="c1"># code in the block</span>
<span class="c1"># more code in the function</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="def my_function_with_block_arg
puts "code in the funtion"
yield
puts "more code in the function"
end
my_function_with_block_arg { puts "code in the block" }
# OUTPUT:
# code in the funtion
# code in the block
# more code in the function
">Copy</button>
</div>
<p>There is an alternate syntax for calling the function: instead
of the curly braces you can use <code>do</code> and <code>end</code>:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="n">my_function_with_block_arg</span> <span class="k">do</span>
<span class="nb">puts</span> <span class="s2">"code in the block"</span>
<span class="nb">puts</span> <span class="s2">"more code in the block"</span>
<span class="k">end</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="my_function_with_block_arg do
puts "code in the block"
puts "more code in the block"
end
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-42'>▻</a>
<h2 id="summary"><a class="anchorlink" href="#summary"><span>10</span> Summary</a></h2><p>You now know about the basic data types, about enumerables and about blocks -
features that distinguish Ruby from other scripting languages.</p><p>If you want to get more practical with Ruby, you can
do the
<a href="https://github.com/backend-development/learn_ruby">Learn Ruby</a> test driven Ruby exercises.</p><p>This should be a good enough basis to start with Rails.
But do take every
opportunity you get to learn more about Ruby itself: if you are unsure about
a line of code, look it up in the Ruby documentation and use the
opportunity to read a bit more than strictly necessary.</p></section>
<section><a class='slide_break' href='ruby_commandline.html#slide-43'>▻</a>
<h3 id="online-resources"><a class="anchorlink" href="#online-resources"><span>10.1</span> Online Resources</a></h3>
<ul>
<li><a href="https://ruby.github.io/TryRuby/">try ruby</a></li>
<li><a href="https://learnbyexample.github.io/learn_ruby_oneliners/one-liner-introduction.html">learn ruby one-liners</a></li>
</ul>
</section>
<section><a class='slide_break' href='ruby_commandline.html#slide-44'>▻</a>
<h3 id="books"><a class="anchorlink" href="#books"><span>10.2</span> Books</a></h3>
<ul>
<li>Flanagan, Matsumoto(2008): <a href="https://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177">The Ruby Programming Language</a>. O'Reilly. ISBN 0596516177.</li>
<li>Thomas(2013): <a href="https://www.amazon.com/Programming-Ruby-1-9-2-0-Programmers/dp/1937785491/">Programming Ruby 1.9 + 2.0</a>. Pragmatic Porgrammers. ISBN 1937785491</li>
<li>Black, Josef (2019): <a href="https://www.amazon.de/Well-Grounded-Rubyist-David-Black/dp/1617295213">The Well Grounded Rubyist</a></li>
<li>Olsen (2011): <a href="https://www.amazon.com/Eloquent-Ruby-Addison-Wesley-Professional/dp/0321584104/">Eloquent Ruby</a>. Addison-Wesley. ISBN 0321584104</li>
<li>Tate(2010): <a href="https://www.amazon.com/Seven-Languages-Weeks-Programming-Programmers/dp/193435659X/">Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages</a>. Pragmatic Programmers. ISBN 193435659X</li>
</ul>
</div></section>
</div>
</div>
<!-- End slides. -->
<!-- Required JS files. -->
<script src="javascripts/reveal.js"></script>
<script src="javascripts/search.js"></script>
<script src="javascripts/markdown.js"></script>
<script>
// Also available as an ES module, see:
// https://revealjs.com/initialization/
Reveal.initialize({
controls: false,
progress: true,
center: false,
hash: true,
// The "normal" size of the presentation, aspect ratio will
// be preserved when the presentation is scaled to fit different
// resolutions. Can be specified using percentage units.
width: 1000,
height: 600,
disableLayout: false,
// Factor of the display size that should remain empty around
// the content
margin: 0.05,
// Bounds for smallest/largest possible scale to apply to content
minScale: 0.2,
maxScale: 10.0,
keyboard: {
27: () => {
// do something custom when ESC is pressed
var new_url = window.location.pathname.replace('slides_', '') + window.location.hash.replace('/','slide-');
window.location = new_url;
},
191: 'toggleHelp',
13: 'next', // go to the next slide when the ENTER key is pressed
},
// Learn about plugins: https://revealjs.com/plugins/
plugins: [ RevealSearch, RevealMarkdown ]
});
</script>
</body>
</html>