-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathruby_commandline.html
More file actions
1112 lines (1067 loc) · 69.1 KB
/
ruby_commandline.html
File metadata and controls
1112 lines (1067 loc) · 69.1 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
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html dir="ltr" lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>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">
<link rel="icon" href="images/backend-development.svg" sizes="any">
<script src="javascripts/@hotwired--turbo.js" data-turbo-track="reload"></script>
<script src="javascripts/clipboard.js" data-turbo-track="reload"></script>
<script src="javascripts/guides.js" data-turbo-track="reload"></script>
<meta property="og:title" content="Ruby — Ruby on Rails Guides" />
<meta name="description" content="RubyThis guide will focus on Ruby - the language - alone.After finishing this guide you will have an overview of Ruby's type system be able to use list processing functions in Ruby be able to use blocks and yield in Ruby. Fork the learn_ruby repository to try some test driven ruby learning" />
<meta property="og:description" content="RubyThis guide will focus on Ruby - the language - alone.After finishing this guide you will have an overview of Ruby's type system be able to use list processing functions in Ruby be able to use blocks and yield in Ruby. Fork the learn_ruby repository to try some test driven ruby learning" />
<meta property="og:locale" content="en_US" />
<meta property="og:site_name" content="Textbook Backend Developemnt" />
<meta property="og:image" content="images/backend-development.svg" />
<meta property="og:type" content="website" />
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Arabic:wght@100..900&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Heebo:wght@100..900&family=Noto+Sans+Arabic:wght@100..900&display=swap" rel="stylesheet">
<meta name="theme-color" content="#2e56e9">
</head>
<body class="guide">
<header id="page_header">
<div class="wrapper clearfix">
<nav id="feature_nav">
<div class="header-logo">
<a href="/">Backend Development</a>
</div>
<ul class="nav">
<li><a class="nav-item" id="home_nav" href="/">Home</a></li>
<li class="guides-index guides-index-large">
<a href="index.html" id="guidesMenu" class="guides-index-item nav-item">Index</a>
<div id="guides" class="clearfix" style="display: none;">
<hr />
<dl class="guides-section-container">
<div class="guides-section">
<dt>Ruby on Rails</dt>
<dd><a href="ruby_commandline.html">Ruby Commandline</a></dd>
<dd><a href="rails_database_and_model.html">Models and ActiveRecord</a></dd>
<dd><a href="rails_db.html">Database and Migrations</a></dd>
<dd><a href="rails_associations_and_validations.html">Associations and Validations</a></dd>
<dd><a href="rails_view_and_controller.html">Routing, View and Controller</a></dd>
<dd><a href="rails_authentication.html">Simple Authentication</a></dd>
<dd><a href="assets_and_import_map.html">The Asset Pipeline and Importmaps</a></dd>
<dd><a href="testing.html">Getting started with Testing</a></dd>
<dd><a href="refactoring_rails.html">Refactoring Rails</a></dd>
<dd><a href="deploy-to-paas.html">Deploy to PAAS</a></dd>
<dd><a href="rails_gems.html">Ruby Gems for your Rails Project</a></dd>
<dd><a href="deploying_rails.html">Deploying Rails</a></dd>
</div>
<div class="guides-section">
<dt>Ruby on Rails - Advanced Topics</dt>
<dd><a href="deploy-to-paas.html">Deploy to PAAS</a></dd>
<dd><a href="rest-api.html">REST API</a></dd>
<dd><a href="graphql-api.html">GraphQL API</a></dd>
<dd><a href="rails_websockets.html">Websocket in Rails</a></dd>
<dd><a href="jobs_and_tasks.html">Jobs and Tasks in Rails</a></dd>
<dd><a href="rails_security.html">Rails Security</a></dd>
</div>
<div class="guides-section">
<dt>Overarching Concerns</dt>
<dd><a href="issue.html">Issue Lifecycle</a></dd>
<dd><a href="security.html">Security</a></dd>
<dd><a href="adv_authentication.html">Advanced Authentication</a></dd>
<dd><a href="caching.html">Caching</a></dd>
<dd><a href="advanced_testing.html">Advanced Testing</a></dd>
<dd><a href="internationalization.html">Internationalization (I18n)</a></dd>
<dd><a href="git_rebasing.html">Git Rebasing</a></dd>
</div>
<div class="guides-section">
<dt>Nodes.js</dt>
<dd><a href="node_vs_rails.html">Node vs. Rails</a></dd>
<dd><a href="node_basics.html">Node Basics</a></dd>
<dd><a href="node_websockets.html">Node Websockets</a></dd>
<dd><a href="node_express.html">Node Web App</a></dd>
<dd><a href="node_cluster.html">Scaling Node</a></dd>
</div>
<div class="guides-section">
<dt>Next.js</dt>
<dd><a href="nextjs.html">Next.js</a></dd>
</div>
</dl>
</div>
</li>
<li class="guides-index guides-index-small">
<select class="guides-index-item nav-item">
<option value="index.html">Index</option>
<optgroup label="Ruby on Rails">
<option value="ruby_commandline.html">Ruby Commandline</option>
<option value="rails_database_and_model.html">Models and ActiveRecord</option>
<option value="rails_db.html">Database and Migrations</option>
<option value="rails_associations_and_validations.html">Associations and Validations</option>
<option value="rails_view_and_controller.html">Routing, View and Controller</option>
<option value="rails_authentication.html">Simple Authentication</option>
<option value="assets_and_import_map.html">The Asset Pipeline and Importmaps</option>
<option value="testing.html">Getting started with Testing</option>
<option value="refactoring_rails.html">Refactoring Rails</option>
<option value="deploy-to-paas.html">Deploy to PAAS</option>
<option value="rails_gems.html">Ruby Gems for your Rails Project</option>
<option value="deploying_rails.html">Deploying Rails</option>
</optgroup>
<optgroup label="Ruby on Rails - Advanced Topics">
<option value="deploy-to-paas.html">Deploy to PAAS</option>
<option value="rest-api.html">REST API</option>
<option value="graphql-api.html">GraphQL API</option>
<option value="rails_websockets.html">Websocket in Rails</option>
<option value="jobs_and_tasks.html">Jobs and Tasks in Rails</option>
<option value="rails_security.html">Rails Security</option>
</optgroup>
<optgroup label="Overarching Concerns">
<option value="issue.html">Issue Lifecycle</option>
<option value="security.html">Security</option>
<option value="adv_authentication.html">Advanced Authentication</option>
<option value="caching.html">Caching</option>
<option value="advanced_testing.html">Advanced Testing</option>
<option value="internationalization.html">Internationalization (I18n)</option>
<option value="git_rebasing.html">Git Rebasing</option>
</optgroup>
<optgroup label="Nodes.js">
<option value="node_vs_rails.html">Node vs. Rails</option>
<option value="node_basics.html">Node Basics</option>
<option value="node_websockets.html">Node Websockets</option>
<option value="node_express.html">Node Web App</option>
<option value="node_cluster.html">Scaling Node</option>
</optgroup>
<optgroup label="Next.js">
<option value="nextjs.html">Next.js</option>
</optgroup>
</select>
</li>
</ul>
</nav>
</div>
</header>
<hr class="hide" />
<section id="feature">
<div class="wrapper">
<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>
<nav id="subCol">
<h3 class="chapter">
<picture>
<!-- Using the `source` HTML tag to set the dark theme image -->
<source
srcset="images/icon_book-close-bookmark-1-wht.svg"
media="(prefers-color-scheme: dark)"
/>
<img src="images/icon_book-close-bookmark-1.svg" alt="Chapter Icon" />
</picture>
Chapters
</h3>
<ol class="chapters">
<li><a href="#what-is-ruby">What is Ruby</a>
<ul>
<li><a href="#what-is-ruby-on-rails">What is Ruby on Rails</a></li>
<li><a href="#why-ruby-questionmark-why-rails-questionmark">Why Ruby? Why Rails ?</a></li>
</ul></li>
<li><a href="#ruby-basics">Ruby Basics</a>
<ul>
<li><a href="#some-code-conventions">Some code conventions</a></li>
</ul></li>
<li><a href="#methods">Methods</a>
<ul>
<li><a href="#definig-a-method">Definig a Method</a></li>
<li><a href="#return-value">Return Value</a></li>
<li><a href="#keyword-arguments">Keyword Arguments</a></li>
</ul></li>
<li><a href="#object-orientation">Object Orientation</a>
<ul>
<li><a href="#the-save-navigation-operator">The 'Save Navigation' Operator</a></li>
</ul></li>
<li><a href="#type-system">Type System</a>
<ul>
<li><a href="#strict-type-checking">Strict Type Checking</a></li>
<li><a href="#strings">Strings</a></li>
<li><a href="#boolean-values">Boolean Values</a></li>
<li><a href="#symbols">Symbols</a></li>
<li><a href="#arrays">Arrays</a></li>
<li><a href="#hashes">Hashes</a></li>
</ul></li>
<li><a href="#frozen-vs-mutable">Frozen vs Mutable</a>
<ul>
<li><a href="#frozen-string-literals">Frozen String Literals</a></li>
</ul></li>
<li><a href="#conditions">Conditions</a>
<ul>
<li><a href="#shorthand-version">Shorthand version</a></li>
<li><a href="#boolean-operators-as-conditions">Boolean Operators as conditions</a></li>
</ul></li>
<li><a href="#enumerables">Enumerables</a>
<ul>
<li><a href="#piping-data">Piping Data</a></li>
<li><a href="#piping-data-in-ruby">Piping Data in Ruby</a></li>
</ul></li>
<li><a href="#blocks">Blocks</a>
<ul>
<li><a href="#my-function-takes-a-block-of-code">My Function takes a Block of Code</a></li>
</ul></li>
<li><a href="#summary">Summary</a>
<ul>
<li><a href="#online-resources">Online Resources</a></li>
<li><a href="#books">Books</a></li>
</ul></li>
</ol>
</nav>
<hr>
</div>
</section>
<main id="container">
<div class="wrapper">
<div id="mainCol">
<div class='slide'>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-0' href='slides_ruby_commandline.html#/0'>◻</a></p>
<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></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-1' href='slides_ruby_commandline.html#/1'>◻</a></p>
<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></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-2' href='slides_ruby_commandline.html#/2'>◻</a></p>
<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></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-3' href='slides_ruby_commandline.html#/3'>◻</a></p>
<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></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-4' href='slides_ruby_commandline.html#/4'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-5' href='slides_ruby_commandline.html#/5'>◻</a></p>
<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></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-6' href='slides_ruby_commandline.html#/6'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-7' href='slides_ruby_commandline.html#/7'>◻</a></p>
<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></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-8' href='slides_ruby_commandline.html#/8'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-9' href='slides_ruby_commandline.html#/9'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-10' href='slides_ruby_commandline.html#/10'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-11' href='slides_ruby_commandline.html#/11'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-12' href='slides_ruby_commandline.html#/12'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-13' href='slides_ruby_commandline.html#/13'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-14' href='slides_ruby_commandline.html#/14'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-15' href='slides_ruby_commandline.html#/15'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-16' href='slides_ruby_commandline.html#/16'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-17' href='slides_ruby_commandline.html#/17'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-18' href='slides_ruby_commandline.html#/18'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-19' href='slides_ruby_commandline.html#/19'>◻</a></p>
<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></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-20' href='slides_ruby_commandline.html#/20'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-21' href='slides_ruby_commandline.html#/21'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-22' href='slides_ruby_commandline.html#/22'>◻</a></p>
<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></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-23' href='slides_ruby_commandline.html#/23'>◻</a></p>
<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></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-24' href='slides_ruby_commandline.html#/24'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-25' href='slides_ruby_commandline.html#/25'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-26' href='slides_ruby_commandline.html#/26'>◻</a></p>
<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></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-27' href='slides_ruby_commandline.html#/27'>◻</a></p>
<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></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-28' href='slides_ruby_commandline.html#/28'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-29' href='slides_ruby_commandline.html#/29'>◻</a></p>
<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></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-30' href='slides_ruby_commandline.html#/30'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-31' href='slides_ruby_commandline.html#/31'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-32' href='slides_ruby_commandline.html#/32'>◻</a></p>
<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></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-33' href='slides_ruby_commandline.html#/33'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-34' href='slides_ruby_commandline.html#/34'>◻</a></p>
<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></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-35' href='slides_ruby_commandline.html#/35'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-36' href='slides_ruby_commandline.html#/36'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-37' href='slides_ruby_commandline.html#/37'>◻</a></p>
<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>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-38' href='slides_ruby_commandline.html#/38'>◻</a></p>
<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>
</div>
<div class='slide'>