forked from cherimarie/gdi-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class2.html
684 lines (585 loc) · 25.1 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
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
<!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/gdicool.css" id="theme">
<!-- For syntax highlighting -->
<!-- light editor--><link rel="stylesheet" href="reveal/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" alt="GDI Logo Badge">
<h3>Intro to Ruby</h3>
<h4>Class 2</h4>
</section>
<section>
<h3>Welcome!</h3>
<div class = "left-align">
<p>Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.</p>
<p class ="green">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>
<li>Text editor, command line, and ruby shell</li>
</ul>
</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 basic data type.</p>
<p>It can have only two values: <strong>true</strong> or <strong>false</strong>.</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 cheat sheet</h3>
<pre><code class="ruby">
a == b # a is equal to b
a != b # a does not equal b
a < b # a is less than b
a > b # a is greater than b
a <= b # a is less than or equal to b
a >= b # a is greater than or equal to b
</code></pre>
<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
a = 3
b = 4
a != b
a <= 3
a >= 4
a = 5
b = 5
a == b
result = a == b # Combine comparison and assignment to variable
puts result
3 < 5
</code></pre>
<p>Remember: Equals and assignment use different operators"</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>!(true && false)</td>
<td>true</td>
</tr>
</table>
<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 || !(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>
<pre><code class="ruby">
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>
<p>Can you tell what this code does?</p>
</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 x == 5
puts 'x is equal to 5'
end
puts 'x is equal to 5' if x == 5
</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 x == 5
puts 'x is equal to 5'
else
puts 'x 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
</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">
# Not
if !(x > y)
puts 'x is less than y'
end
puts 'x is less than y' if !(x > y)
puts 'x is less than y' unless x > y
</code></pre>
<p>The parentheses allow us to group expressions and negate them.</p>
</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 x > 10
puts "x is greater than 10"
elsif x <= 10 && x > 0
puts "x is a number between 1 and 10"
else
puts "Wow, don't be so negative!"
end
</code></pre>
</section>
<!-- Let's develop it: 10 minutes -->
<section>
<h3>Let's Develop It</h3>
<p>Make a file named adventure.rb.</p>
<pre><code class="ruby">
# navigate to your hard drive
cd ~/ # Linux, Mac
cd C:\ # Windows
# navigate into the code directory
cd code
# make and navigate into a new directory for this class
mkdir gdi_class2
cd gdi_class2
# create a file
touch adventure.rb
</code></pre>
<p>Run your program by calling it with Ruby from the command line.</p>
<pre><code>
ruby adventure.rb
</code></pre>
</section>
<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 a simple 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>
<p>Write out your program in prose, then translate each line into Ruby</p>
<pre><code>
input = gets.chomp.to_i
</code></pre>
</section>
<section>
<h3>Let's Develop It Example</h3>
<pre><code class="ruby">
# adventure.rb
puts "A vicious dragon is chasing you!"
puts "Options:"
puts "1 - Hide in a cave"
puts "2 - Climb a tree"
input = gets.chomp
if input == '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 input == '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">
#command line
ruby adventure.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">
x = 10
while x > 0
puts "Loop number #{x}"
x = x - 1
end
puts 'Done'
</code></pre>
</section>
<section>
<h3>While Loops</h3>
<pre><code class="ruby">
x = 10
while x > 0
puts "Loop number #{x}"
x = x - 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 'x = x - 1' statement to decrement the value of x, 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><strong>#{num_bottles}</strong> 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>
<p>Make a new file called <strong>loopy.rb</strong> in the folder for today's class and add this code.</p>
<pre><code class="ruby">
# 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 |number|
puts "Value of number is #{number}"
end
</code></pre>
</section>
<section>
<h3>Each loops</h3>
<pre><code class="ruby">
(0..5).each do |number|
puts "Value of number is #{number}"
end
</code></pre>
<p>The loop has three parts:</p>
<ul>
<li>The collection that will be looped through, in this case a range- '(0..5)'</li>
<li>The name to give each element when the loop begins again - 'number' - between the pipes | | </li>
<li>The code to execute with the element - puts line</li>
</ul>
<br />
<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</li>
</ul>
<pre><code class="ruby">
new_range = (1..10)
new_array = [1, 3, 5]
new_hash = {"dog" => "snoopy", "bird" => "woodstock"}
</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 simply 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. <strong>Exclusive</strong> ranges have three dots, and do not include the last element.</p>
<p>Ranges need to 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
(28..22).min
('a'...'g').each do |letter|
puts "#{letter} is a pretty good letter"
end
(22..28).to_a
</code></pre>
<p>Learn more about ranges <a href="http://www.ruby-doc.org/core-2.1.1/Range.html" alt="Ruby Doc">here</a>.</p>
</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">
new_array = [1, 3, 5, 89, 212, 7, -100]
arr = ["wow", "woooo", "zowie"]
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
new_array.length
new_array.count
arr.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">
new_array = [1, 3, 5, 89, 212, 7, -100]
arr = ["wow", "woooo", "zowie"]
new_array[0] # returns the zeroth element
arr[2] # returns the third element
arr[-1] # returns the last (1st from the end) element
new_array.last # returns the last element
new_array[1..2] # returns the second and third elements
arr.first # returns the first element
</code></pre>
</section>
<section>
<h3>Adding and 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">
arr = ["wow", "woooo", "zowie"]
#add
arr.push("hot diggity") # adds argument as last element
arr << "yikes" # adds argument as last element
#remove
arr.delete("wow") # deletes the element that matches argument
arr.pop # removes and returns the last 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
arr.join # returns a string made up of all the elements
arr.clear # removes all elements from the array
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
</code></pre>
<p>Learn more about arrays <a href="http://www.ruby-doc.org/core-2.1.0/Array.html" alt="ruby documentation - array class">here</a>.</p>
</section>
<!-- minutes-->
<section>
<h3>Hashes</h3>
<p>Hashes have curly braces and hash rockets and can be filled with any data type: integers, floats, strings, even arrays and hashes. </p>
<pre><code class="ruby">
grades_hash = { "Jane Doe" => 10, "Jim Doe" => 6, "Jan Doe" => 8}
new_hash = { 1 => "a", "d" => 12, "f" => 35 }
# methods to find information about hashes
new_hash.length
grades_hash.count
grades_hash.size
grades_hash.has_key?("Jan Doe")
new_hash.has_value?(35)
</code></pre>
<p>Syntax:</p>
<p>{ key => value, key => value }
</section>
<section>
<h3>Accessing Elements in Hashes</h3>
<p>Hashes are <strong>unordered.</strong> Hashes are like dictionaries, with unique <strong>key / value</strong> pairs.</p>
<p>Because hashes can have any type of object as an index, and are unordered, we must access values by their <strong>key</strong>.</p>
<pre><code class="ruby">
grades_hash = { "Jane Doe" => 10, "Jim Doe" => 6, "Jan Doe" => 8}
new_hash = { 1 => "a", "d" => 12, "f" => 35 }
grades_hash["Jane Doe"] # returns 10, the value of this key
new_hash["d"] # returns 12, the value of this key
grades_hash.first # returns a key/value pair
</code></pre>
</section>
<section>
<h3>Adding and Removing from Hashes</h3>
<pre><code class="ruby">
new_hash = { 1 => "a", "d" => 12, "f" => 35 }
# add
new_hash["z"] = 43 # adds a new key/value pair "z" => 43
#remove
new_hash.delete("d") # removes key/value pair with specified key
new_hash.clear # removes all key/value pairs
</code></pre>
</section>
<section>
<h3>More Hash methods</h3>
<pre><code class="ruby">
chapters = {"My Early Home" => (1..15), "The Hunt" => (16..28),
"My Breaking In" => (29..46), "Birtwick Park" => (46..60)}
chapters.count # returns number of key/value pairs in hash
chapters.keys # returns an array of all the keys in hash
chapters.has_key?("How It Ended") # returns a boolean
chapters.to_a # converts hash to an array of arrays
chapters.invert # returns new hash with old one's values
# as keys and keys as values
</code></pre>
<p>Learn more about hashes <a href="http://www.ruby-doc.org/core-2.1.1/Hash.html" alt="Ruby Docs">here</a>.</p>
</section>
<!-- minutes -->
<section>
<h3>Questions?</h3>
</section>
<section>
<h3>Homework</h3>
<p><strong>Review:</strong> Read sections 7, 8 & 9 of <a href="http://tutorials.jumpstartlab.com/projects/ruby_in_100_minutes.html" alt="Ruby in 100 Minutes.">Ruby in 100 Minutes.</a></p>
<p><strong>Practice:</strong> Write the code for the deaf grandma exercise you wrote prose for last week.<a href="http://pine.fm/LearnToProgram/?Chapter=06" alt="Chp 6 of Learn to Program">Chapter 6</a> of Learn to Program.</p>
<p><strong>Prep:</strong> Read <a href="http://pine.fm/LearnToProgram/?Chapter=08" alt="Chp 8 of Learn to Program">Chapter 8</a> of Learn To Program- don't try to do the exercises at the end yet, though.</p>
</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>