forked from cherimarie/gdi-ruby
/
class2.html
588 lines (521 loc) · 23.7 KB
/
class2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Intro to Ruby ~ Girl Develop It</title>
<meta name="description" content="This is the official Girl Develop It Core Intro to Ruby course. The course is meant to be taught in four two-hour sessions. Each of the slides and practice files are customizable according to the needs of a given class or audience.">
<meta name="author" content="Girl Develop It">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="reveal/css/reveal.css">
<link rel="stylesheet" href="reveal/css/theme/gdisunny.css" id="theme">
<!-- For syntax highlighting -->
<!-- light editor<link rel="stylesheet" href="lib/css/light.css">-->
<!-- dark editor-->
<link rel="stylesheet" href="reveal/lib/css/dark.css">
<!-- If use the PDF print sheet so students can print slides-->
<link rel="stylesheet" href="reveal/css/print/pdf.css" type="text/css" media="print">
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!-- Opening slide -->
<section>
<img src = "images/gdi_logo_badge.png">
<h3>Intro to Ruby</h3>
<h4>Class 2</h4>
</section>
<section>
<h3>Welcome Back!</h3>
<div>
<p>Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.</p>
<p class ="blue">Reminder! Some "rules":</p>
<ul>
<li>We are here for you!</li>
<li>Every question is important.</li>
<li>Help each other.</li>
<li>Have fun!</li>
</ul>
</div>
</section>
<!-- Homework Review - 10 min -->
<section>
<h3>Homework Discussion</h3>
<p>How was last week's homework? Do you have any questions or concepts that you'd like to discuss?</p>
</section>
<!-- Block 1 30 minutes -->
<section>
<h3>Review</h3>
<ul>
<li>Arithmetic and variables</li>
<li>Data types</li>
</ul>
</section>
<section>
<h3>IRB</h3>
<li class="fragment">IRB ("interactive ruby") is a command line interface to Ruby that is installed when we install Ruby. It works like a calculator and is a great way to get started quickly. This is a lot like the Repl we used in Class 1.
<li class="fragment">Open: In your terminal, type `irb`</li>
<li class="fragment">Quit: Type `quit` and then press `enter`</li>
</section>
<section>
<h3>What we will cover today</h3>
<ul>
<li class ="fragment">Boolean Expressions and Conditionals</li>
<li class ="fragment">Loops</li>
<li class ="fragment">Collections</li>
</ul>
</section>
<section>
<h3>Booleans</h3>
<p>A <strong>boolean</strong> is a data type. It can have only two values: true or false.</p>
</section>
<section>
<h3>Boolean Expressions</h3>
<p>Code that compares values and returns True or False is called a <strong>Boolean expression</strong></p>
<ul>
<li>Test for equality by using `==`. We can't use `=` because that is used for assignment</li>
<li>Test for greater than and less than using `>` and `<`</li>
</ul>
</section>
<section>
<h3>Boolean Expressions cheat sheet</h3>
<table>
<tr>
<td>a == b</td>
<td>a is equal to b</td>
</tr>
<tr>
<td>a != b</td>
<td>a does not equal b</td>
</tr>
<tr>
<td>a < b</td>
<td>a is less than b</td>
</tr>
<tr>
<td>a > b</td>
<td>a is greater than b</td>
</tr>
<tr>
<td>a <= b</td>
<td>a is less than or equal to b</td>
</tr>
<tr>
<td>a >= b</td>
<td>a is greater than or equal to b</td>
</tr>
</table>
<p>Learn more about logical operators in Ruby <a href="http://www.tutorialspoint.com/ruby/ruby_operators.htm" alt="Operators tutorial">here</a>.</p>
</section>
<section>
<h3>Boolean Expressions practice</h3>
<pre><code class="ruby">
# try some of these out in IRB
first_num = 3
second_num = 4
first_num != second_num
first_num <= 3
first_num >= 4
first_num = 5
second_num = 5
first_num == second_num
# Combine comparison and assignment
nums_are_equal = (first_num == second_num)
puts nums_are_equal
</code></pre>
<p>Remember: Equals does not equal "equals equals"</p>
<p> <a href="http://ruby.learncodethehardway.org/book/ex27.html">Further reading </a> on boolean expressions...</p>
</section>
<section>
<h3>Boolean Expressions</h3>
<p>A boolean expression evaluates to true or false. It can also have multiple parts, joined by AND (&&) or OR (||).</p>
<table>
<tr>
<th>EXPRESSION</th>
<th>EVALUATES TO</th>
</tr>
<tr>
<td>true && true</td>
<td>true</td>
</tr>
<tr>
<td>true && false</td>
<td>false</td>
</tr>
<tr>
<td>false && false</td>
<td>false</td>
</tr>
<tr>
<td>true || true</td>
<td>true</td>
</tr>
<tr>
<td>true || false</td>
<td>true</td>
</tr>
<tr>
<td>false || false</td>
<td>false</td>
</tr>
<tr>
<td>not (true && false)</td>
<td>true</td>
</tr>
</table>
<p>Ruby operators and boolean expressions have <a href="https://gist.github.com/tian-im/3877402">precendence rules!</a></p>
<p><a href="http://ruby.learncodethehardway.org/book/ex28.html">Further practice </a> on boolean expressions</p>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Take a few minutes and experiment with boolean expressions in IRB. You can start with the examples below.</p>
<pre><code class="ruby">
true && false
1 == 1 && 2 > 37
"boop" == "bip" || 7 == 8
false || true
89 > 88 || 89 < 90
true || not(1 == 1 || 2 == 65)
</code></pre>
</section>
<section>
<h3>Putting Booleans to Work</h3>
<p>So, what's the value of knowing if a statement is true or false? Often, you'll use that to control whether a piece of code will execute or not.</p>
<p>Can you tell what this code does?</p>
<pre><code class="ruby">
print "Guess my secret number: "
user_guess = gets.chomp.to_i
secret_number = 312
if user_guess < secret_number
puts "Too low!"
elsif user_guess > secret_number
puts "Too high!"
else
puts "You guessed it! Wow, maybe you're psychic...."
end
</code></pre>
</section>
<section>
<h3>Conditionals</h3>
<p>When we want different code to execute depending on certain criteria, we use a <strong>conditional</strong></p>
<p>We achieve this using <strong>if</strong> statements and boolean expressions.</p>
<pre><code class="ruby">
if number == 5
puts 'number is equal to 5'
end
</code></pre>
</section>
<section>
<h3>Conditionals</h3>
<p>We often want a different block to execute if the statement is false. This can be accomplished using <strong>else</strong>.</p>
<pre><code class="ruby">
if number == 5
puts 'number is equal to 5'
else
puts 'number is not equal to 5'
end
</code></pre>
</section>
<section>
<h3>Conditionals</h3>
<p>The following shows some examples of conditionals with more complex boolean expressions:</p>
<pre><code class="ruby">
# And
if x > 3 && y > 3
puts 'Both values are greater than 3'
end
# Or
if x != 0 || y != 0
puts 'The point x,y is not on the x or y axis'
end
# Not
if not(x > y)
puts 'x is less than or equal to y'
end
</code></pre>
</section>
<section>
<h3>Chained conditionals</h3>
<p>Conditionals can also be <strong>chained</strong>.</p>
<p>Chained conditionals use <strong>elsif</strong> to test if additional statements are true. The single 'else' action will only happen if all preceding conditions are false.</p>
<p>For example:</p>
<pre><code class="ruby">
if number > 10
puts "number is greater than 10"
elsif number <= 10 && number > 0
puts "number is a number between 1 and 10"
else
puts "Wow, don't be so negative!"
end
</code></pre>
</section>
<section>
<h3>Let's Develop It!</h3>
<pre><code class="ruby">
# adventure.rb
puts "A vicious dragon is chasing you!"
puts "Your choices:"
puts "1 - Hide in a cave"
puts "2 - Climb a tree"
user_choice = gets.chomp
if user_choice == '1'
puts "You hide in a cave. The dragon finds you and asks if you'd like to play Scrabble. Maybe it's not so vicious after all!"
elsif user_choice == '2'
puts "You climb a tree. The dragon can't find you."
else
puts "That's not a valid option."
end
</code></pre>
<pre><code class="ruby">
# run the file from your terminal
ruby adventure.rb
</code></pre>
</section>
<!-- Let's develop it: 10 minutes -->
<section>
<h3>Let's Develop It!</h3>
<p>Write a program in your text editor that uses conditionals and user input to allow the user to play an adventure game.</p>
<p>"gets.chomp" is the value of user input at the command line, with the trailing whitespace chomped off. To do math with it, convert it to an integer with the ".to_i" method</p>
<pre><code>
user_input = gets.chomp.to_i
</code></pre>
<p>Run your program by calling it with Ruby from the command line.</p>
<pre><code>
$ ruby program_name.rb
</code></pre>
</section>
<!-- Block 2 25 minutes -->
<section>
<h3>Loops</h3>
<p>It is often useful to perform a task and to repeat the process until a certain point is reached.</p>
<p>The repeated execution of a set of statements is called <strong>iteration</strong>, or, more commonly, a loop.</p>
<p>One way to achieve this, is with the <strong>while</strong> loop.</p>
<pre><code class="ruby">
number = 10
while number > 0
puts "Loop number #{number}"
number = number - 1
end
puts 'Done!'
</code></pre>
</section>
<section>
<h3>While Loops</h3>
<pre><code class="ruby">
number = 10
while number > 0
puts "Loop number #{number}"
number = number - 1
end
</code></pre>
<p>The while statement takes a condition, and as long as it evaluates to true, the code block beneath it is repeated. This creates a <strong>loop</strong>.</p>
<p> Without the `number = number - 1` statement, to decrement the value of `number`, this would be an <strong>infinite loop</strong> :( :( :(</p>
</section>
<section>
<h3>While loops</h3>
<p>Consider the following example that uses a while loop to sing you a song.</p>
<pre><code class="ruby">
num_bottles = 99
while num_bottles > 0
puts "#{num_bottles} bottles of beer on the wall,
#{num_bottles} bottles of beer, take one down, pass it
around, #{num_bottles - 1} bottles of beer on the wall!"
num_bottles = num_bottles - 1
end
</code></pre>
<p>"#{num_bottles}" is an example of <strong>string interpolation</strong></p>
</section>
<!-- Let's Develop It - 10 minutes -->
<section>
<h3>Let's Develop It</h3>
<ul>
<li>Write a program that obtains user input like the last program</li>
<li>This program should not exit until the user says it should (maybe by entering "quit"?)</li>
<li>Use a loop!</li>
<li>You can use the next slide as an example.</li>
</ul>
</section>
<section>
<h3>Let's Develop It: Example</h3>
<pre><code>
# loopy.rb
loopy = true
while loopy == true
puts "Are we having fun yet?"
puts "Answer 'true' or 'false'."
user_input = gets.chomp
if user_input == 'false'
loopy = false
end
end
</code></pre>
<p>Learn more about loops in Ruby <a href="http://www.tutorialspoint.com/ruby/ruby_loops.htm" alt="Loops tutorial">here</a>.</p>
</section>
<section>
<h3>Each loops</h3>
<p>The most commonly used type of loop in Ruby is an each loop. It uses the <strong>.each</strong> method to iterate over a collection of elements, doing work to each one.</p>
<p>First, we need a collection. Let's use a <strong>range</strong> of numbers to loop over.</p>
<pre><code class="ruby">
(0..5).each do |num|
puts "Value of num is #{num}"
end
</code></pre>
</section>
<section>
<h3>Each loops</h3>
<pre><code class="ruby">
(0..5).each do |num|
puts "Value of num is #{num}"
end
</code></pre>
<p>The loop has three parts:</p>
<p>The collection that will be looped through, in this case a range of "(0..5)"</p>
<p>The name to give each element when the loop begins again - "num" - in the pipes "| |" </p>
<p>The code to execute with the element - the puts statement</p>
<p>We will revisit the each loop when we have a better understanding of collections.</p>
</section>
<!-- Block 3 30 minutes -->
<section>
<h3>Collections</h3>
<p>There are three main types:</p>
<ul>
<li>Ranges</li>
<li>Arrays</li>
<li>Hashes (we will get to these next time)</li>
</ul>
<pre><code class="ruby">
new_range = (1..10)
new_array = [1, 3, 5]
</code></pre>
</section>
<section>
<h3>Ranges</h3>
<pre><code class="ruby">
inclusive_range = (1..3) # contains 1, 2, 3
exclusive_range = (1...3) # contains 1, 2
letter_range = ('a'..'e') # contains 'a', 'b', 'c', 'd', 'e'
</code></pre>
<p>Ranges are the range of values between the given first and last elements.</p>
<p><strong>Inclusive</strong> ranges have two dots, and include the last element.</p>
<p><strong>Exclusive</strong> ranges have three dots, and do not include the last element.</p>
<p>Ranges must be defined from lowest value to highest.</p>
</section>
<section>
<h3>Ranges</h3>
<p>Try out these range methods in IRB.</p>
<pre><code>
(1..99).max
('b'...'z').include?('j')
(890..902).begin
(890..902).first(4)
(890..902).last(3)
(890..902).end
(22..28).min
('a'...'g').each do |letter|
puts "#{letter} is a pretty good letter"
end
(22..28).to_a
</code></pre>
</section>
<section>
<h3>Arrays</h3>
<p>Arrays have square brackets and can be filled with any type of object: integers, floats, strings, even other arrays or hashes.</p>
<pre><code class="ruby">
num_array = [1, 3, 5, 89, 212, 7, -100]
string_array = ["wow", "woooo", "zowie"]
new_array = Array.new # will have no elements inside it initially
varied_array = ["one", 2, "THREE", 0.4, ["five", 6]]
# methods to get information about an array
num_array.length
string_array.include?("yee ha")
</code></pre>
<p>Arrays are a great way to keep track of information that changes frequently.</p>
</section>
<section>
<h3>Accessing Elements in an Arrays</h3>
<p>Arrays are <strong>ordered</strong> and are <strong>integer-indexed</strong>, starting at 0. </p>
<p>Elements can be accessed by their position.</p>
<pre><code class="ruby">
num_array = [1, 3, 5, 89, 212, 7, -100]
string_array = ["wow", "woooo", "zowie"]
num_array[0] # returns the zeroth element
string_array[2] # returns the third element
string_array[-1] # returns the last element
num_array.last # returns the last element
num_array[1..2] # returns the second and third elements
string_array.first # returns the first element
</code></pre>
</section>
<section>
<h3>Adding & Deleting From Arrays</h3>
<p>Adding and removing items to an array can be done in a variety of ways. These are the most common.</p>
<pre><code class="ruby">
string_array = ["wow", "woooo", "zowie"]
# add
string_array.push("hot diggity") # adds argument as last element
string_array << "yikes" # adds argument as last element
# remove
string_array.delete("wow") # deletes the element that matches argument
string_array.pop # removes and returns the last element
string_array.shift # removes and returns the first element
</code></pre>
</section>
<section>
<h3>More Array Methods</h3>
<p>Arrays are used a lot in Ruby. There are a lot of cool methods available for them. </p>
<pre><code class="ruby">
arr = ["dog", "cat", "turtle", "parakeet", "ferret"]
arr.index("dog") # returns the index of the element that matches argument
arr.join # returns a string made up of all the elements
arr.reverse # returns new array with same elements, reversed
arr.shuffle # returns new array with same elements, shuffled
arr.uniq # returns a new array with only unique elements
arr.size # returns the number of elements in the array
arr.empty? # returns a boolean
arr.clear # removes all elements from the array
</code></pre>
<p>Learn more about arrays <a href="http://ruby-doc.org/core-2.4.1/Array.html" alt="ruby documentation - array class">here</a>.</p>
</section>
<section>
<section>
<h3>Homework</h3>
<p>The Ada Developers Academy Jumpstart Curriculum has two great assignments for practicing conditional statements and arrays!</p>
<p><strong>Conditional Statement Practice:</strong> <a href="https://github.com/Ada-Developers-Academy/jump-start/blob/a8e732bb446c43daff5c6123886b464b795eb531/learning-to-code/programming-expressions/README.md#candy-machine-assignment">Candy Machine</a></p>
<p><strong>Array & Loop Practice:</strong> <a href="https://github.com/Ada-Developers-Academy/jump-start/blob/master/learning-to-code/arrays/assignments/account-generator.md">Student Account Generator</a> (note that this uses a new kind of loop, the <strong>times</strong> loop. Search online for what this is. If you can't find it, use another loop!)
</section>
</div>
<footer>
<div class="copyright">
Intro to ruby ~ Girl Develop It ~
<a rel="license" href="http://creativecommons.org/licenses/by-nc/3.0/deed.en_US"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc/3.0/80x15.png" /></a>
</div>
</footer>
</div>
<script src="reveal/lib/js/head.min.js"></script>
<script src="reveal/js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
theme: Reveal.getQueryHash().theme, // available theme are in /css/themes
transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'reveal/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'reveal/plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal/plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'reveal/plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'reveal/plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>