forked from cherimarie/gdi-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class3.html
602 lines (522 loc) · 20.1 KB
/
class3.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
<!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">
<!-- 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">
<div class="slides">
<section>
<img src="images/gdi_logo_badge.png" alt="GDI Logo Badge">
<h3>Intro to Ruby</h3>
<h4>Class 3</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>
<!-- <section>
<h3>HW: grandma.rb</h3>
<pre><code contenteditable class="ruby">
chatting = true
byes = 0
while chatting
puts "Ask Grandma a question:"
said = gets.chomp!
if said == "BYE"
byes += 1
if byes > 2
chatting = false
else
puts "** Grandma is ignoring you **"
end
elsif said == said.upcase
year = 1930 + Random.rand(20)
puts "NO, NOT SINCE #{year}!"
else
puts "HUH?! SPEAK UP, SONNY!"
end
end
puts "Thanks for chatting with Grandma"
</code></pre>
</section>
-->
<!-- <section>
<h3>HW: leap.rb</h3>
<pre><code contenteditable class="ruby">
puts "Enter a starting year in the format YYYY"
starting = gets.chomp!.to_i
puts "Enter an ending year in the format YYYY"
ending = gets.chomp!.to_i
puts "LEAP YEARS between #{starting} and #{ending}"
starting.upto(ending) do |year|
divisible_by_4 = (year % 4) == 0
not_divisible_by_100 = (year % 100) != 0
divisible_by_400 = (year % 400) == 0
is_leap_year = (divisible_by_4 && not_divisible_by_100)
|| divisible_by_400
if is_leap_year
puts "#{year}"
end
end
</code></pre>
</section> -->
<section>
<h3>Review</h3>
<ul>
<li class="fragment">Conditions - if, elsif, else</li>
<li class="fragment">Loops - while, each</li>
<li class="fragment">Ranges - (0..10)</li>
<li class="fragment">Arrays - [1,2,3]</li>
<li class="fragment">Hashes - {"one" => 1, "two" => 2}</li>
</ul>
</section>
<section>
<h3>What we will cover today</h3>
<ul>
<li class="fragment">Enumerable Methods on Collections- .each()</li>
<li class="fragment">Methods</li>
<li class="fragment">Objects</li>
<li class="fragment">Object Oriented Programming</li>
</ul>
</section>
<section>
<h3>Enumerable</h3>
<p><strong>Arrays</strong> and <strong>Hashes</strong> are both classes that have <strong>Enumerable</strong> methods. There are a lot of cool methods available for Enumerable, including <strong>each</strong>. </p>
<!-- <p>Learn more about the methods available to Enumerable <a href="http://www.ruby-doc.org/core-2.1.1/Enumerable.html" alt="Ruby Docs">here</a>.</p> -->
</section>
<section>
<h3>.each with Ranges</h3>
<p>This is the simplest!</p>
<pre><code class="ruby">
(3..8).each do |number|
puts number * 4
end
</code></pre>
</section>
<section>
<h3>.each with Arrays</h3>
<p>Can be simple or more complex, depending on the array.</p>
<pre><code class="ruby">
pets_array = ["dog", "cat", "turtle", "parakeet", "ferret"]
pets_array.each do |pet|
puts "I like #{pet}s!!"
end
deep_array = [["mountain", "desert"], ["ocean", "sky"]]
deep_array.each do |array|
puts "I like #{array.first}s!!" # each element is also an array
end
</code></pre>
</section>
<section>
<h3>.each with Hashes</h3>
<p>Each element has a key and value that needs to be used.</p>
<pre><code class="ruby">
grades_hash = { "Jane Doe" => 10, "Jim Doe" => 6, "Jan Doe" => 8}
grades_hash.each do |key, value|
puts "#{key}'s grade is #{value}"
end
</code></pre>
</section>
<!-- Let's develop it: 10 minutes -->
<section>
<h3>Let's Develop It</h3>
<p>Write a program called 'divisible.rb' that prints out every number between 1 and 1000 that is divisible by 7.</p>
<br />
<p>Make a new ruby file called divisible.rb</p>
<br />
<p>Hint: the modulo operator, %, will be helpful.</p>
<p>Write out your program in prose, then translate each line into Ruby</p>
<pre><code class="ruby">
# try these expressions in IRB
9 / 2
9 % 2
</code></pre>
</section>
<!-- we found that there wasn't enough time in this class for this content, but it may be useful as homework or additional material for advanced students.
<section>
<h3>Enumerable</h3>
<p>Visit <a href="https://gist.github.com/cherimarie/11004115" alt="Gist with pets array">this page</a> and copy the contents of the Gist to a local file. Save it as pets.rb. We'll work with it together in IRB.</p>
</section>
<section>
<h3>Enumerable: Reduce</h3>
<p>Returns the product of running the operator on all elements of the enumerable.</p>
<pre><code class="ruby">
(1..4).reduce(:*) # returns 1 * 2 * 3 * 4
(1...5).reduce(:+) # returns 1 + 2 + 3 + 4
(2...5).reduce(:**) # returns 2 ** 3 ** 4
</code></pre>
</section>
<section>
<h3>Enumerable: Collect and Map</h3>
<p>Returns an array with the results of running the block on each element. Can be used interchangably.</p>
<pre><code class="ruby">
$pets.collect{|p| p[:name]} # returns array of all pet names
$pets.collect{|p| p[:legs]}.reduce(:+) # returns sum of all pet legs
</code></pre>
</section>
<section>
<h3>Enumerable: Reject</h3>
<p>Returns an array with all elements that did not meet the critera in code block.</p>
<pre><code class="ruby">
$pets.reject{|p| p[:talents].include? "napping" }
# returns array of pets whose talents do not include napping
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>How many of our pets are nocturnal?</p>
</section>
<section>
<h3>Let's Develop It- Sample Code</h3>
<p>How many of our pets are nocturnal?</p>
<pre><code class="ruby">
$pets.reject{|p| p[:nocturnal]== false}.count
</code></pre>
</section> -->
<section>
<h3>Methods</h3>
<p>A <strong>method</strong> is a name for a chunk of code that goes together.</p>
<p>Methods are often used to define reusable behavior.</p>
<p class="fragment">Let's look at a method we've already used:</p>
<pre><code contenteditable class="fragment ruby">
1.even?
=> false
"fred".capitalize
=> "Fred"
</code></pre>
<p class="fragment">If you have some code you want to reuse or organize, methods will help.</p>
</section>
<section>
<h3>Let's Develop It!</h3>
<p>Find the methods available on <a href="http://www.ruby-doc.org/core-2.1.0/String.html">String</a> and <a href="http://www.ruby-doc.org/core-2.1.0/Integer.html">Integer</a></p>
<p>Do they have any similar methods?</p>
<pre><code contenteditable class=" fragment ruby">
"abc".next
=> "abd"
"abz".next
=> "aca"
1.next
=> 2
-1.next
=> 0
</code></pre>
</section>
<section>
<h3>Write a Method</h3>
<p>Let's write a method together</p>
<p><strong>def</strong> means define.</p>
<p><strong>subtract</strong> is the name of the method.</p>
<p><strong>x, y</strong> are parameters.</p>
<p><strong>x - y</strong> is the body of the method.</p>
<p><strong>x - y</strong> is also the return value.</p>
<pre><code contenteditable class="ruby">
def subtract(x, y)
x - y
end
</code></pre>
</section>
<section>
<h3>Let's Develop It!</h3>
<p>Let's write a method that takes in a number and tells us if it is even.</p>
<p>Hint: if number % 2 == 0, then it is even.</p>
<pre><code contenteditable class="ruby">
def method_name(parameter)
# method implementation
end
</code></pre>
</section>
<section>
<h3>Method Return Value</h3>
<p>Every Ruby method returns something.</p>
<p>Usually, it's the last statement in the method.</p>
<p>It could be the value sent to <strong>return</strong> if that came first.</p>
<pre><code contenteditable class="ruby">
def subtract(x,y)
x - y
'another thing'
end
return_value = subtract(5, 2)
puts return_value
def subtract(x,y)
return x - y
'another thing'
end
return_value = subtract(5, 2)
puts return_value
</code></pre>
</section>
<section>
<h3>Arguments vs. Parameters</h3>
<p>Technically speaking, arguments are passed and parameters are declared.</p>
<p>Note that the variable names don't have to match!</p>
<p>In this code, 5 is an argument and x is a parameter.</p>
<pre><code contenteditable class="ruby">
def subtract(x,y)
x - y
end
subtract(5, 2)
</code></pre>
</section>
<section>
<h3>Splat Arguments</h3>
<pre><code contenteditable class="ruby">
def greet(greeting, *names)
names.each do |name|
puts "#{greeting}, #{name}!"
end
end
greet("Hello", "Alice", "Bob", "Charlie")
Hello, Alice!
Hello, Bob!
Hello, Charlie!
</code></pre>
</section>
<section>
<h3>Default Values</h3>
<pre><code contenteditable class="ruby">
def eat(food = "chicken")
puts "Yum, #{food}!"
end
eat()
Yum, chicken!
eat("arugula")
Yum, arugula!
</code></pre>
</section>
<section>
<h3>Default Hash Parameter</h3>
<pre><code contenteditable class="ruby">
def add_to_x_and_y(amount, vals)
x = vals[:x]
y = vals[:y]
x + y + amount
end
add_to_x_and_y(2, {:x => 1, :y => 2})
</code></pre>
</section>
<section>
<h3>Named Parameters</h3>
<p>To pass variable parameters, or to pass named parameters, you can use an options hash</p>
<pre><code contenteditable class="ruby">
def bake(name, options = {})
flour = options[:flour] || "rye"
creamer = options[:creamer] || "cream"
puts "baking a nice #{flour} loaf with #{creamer}"
end
bake("Wheat")
bake("Sourdough", :flour => "sour")
bake("Pumpernickel", :creamer => "butter")
</code></pre>
</section>
<section>
<h3>Methods & Variables</h3>
<p><strong>Local vars</strong> are only available in the methods where they were defined.</p>
<pre><code contenteditable class="ruby">
# Let's extract the length of the padded string into
# a local variable
def message(our_string)
length = 80
puts our_string.center(length, "-")
end
def different_message(some_string)
puts our_string.center(length, "=")
end
# Error! 'our_string' and 'length' are not defined inside this method
</code></pre>
</section>
<section>
<h3>Objects</h3>
<p>An <strong>Object</strong> is the building block of a Ruby program.</p>
<br />
<p class="fragment">Objects <strong>do things</strong> and have <strong>properties</strong>.</p>
</section>
<section>
<h3>Objects</h3>
<br />
<h5 class="fragment">Object: Number</h5>
<p class="fragment"><strong>What can it do?</strong></p>
<ul class="fragment">
<li> Add and subtract</li>
<li> Multiply and divide</li>
<li> And more...</li>
</ul>
<p class="fragment"><strong>What are some properties of Number?</strong></p>
<ul class="fragment">
<li> Length</li>
<li> Base</li>
<li> Even? or odd?</li>
<li> And more...</li>
</ul>
</section>
<section>
<h3>Object Class</h3>
<p><strong>Objects</strong> can be grouped by Class.</p>
<br />
<p class="fragment">Classes define a template for objects. They describe what a certain type of object can do and what
properties they have in common.
</p>
<br />
<p class="fragment">There are many kinds of objects, including String, Number, Array, Hash, Time, ...</p>
</section>
<section>
<h3>Object Class</h3>
<p>To create an object we use .new</p>
<pre><code contenteditable class="fragment ruby">
array = Array.new
</code></pre>
<p class="fragment">'array' now refers to an object instance</p>
</section>
<section>
<h3>Object Methods</h3>
<p>Methods can be defined on objects.</p>
<pre><code contenteditable class="fragment ruby">
array = Object.new
def array.delete
puts "deleted the array"
end
array.methods(false)
</code></pre>
</section>
<section>
<h3>Instance Variables</h3>
<p>Represent object state</p>
<p>Names start with an @</p>
<p>Only visible inside the object</p>
<pre><code contenteditable class="fragment ruby">
cookie = Object.new
def cookie.add_chips(num_chips)
@chips = num_chips
end
def cookie.yummy?
@chips > 100
end
cookie.add_chips(500)
cookie.yummy? #=> true
</code></pre>
</section>
<section>
<h3>Object Oriented Programming</h3>
<p>Object Oriented Programming is one of many <strong>programming approaches</strong>: procedural, event-driven, functional, declarative...</p>
<br />
<p class="fragment">These days, most programming is done in an OO language: Java, C#, C++.</p>
</section>
<section>
<h3>Object Oriented Programming</h3>
<p>All this means is that programs are <strong>organized around objects</strong> (data & methods).</p>
<br />
<p class="fragment">You might choose OOP because Objects make things <strong>easier to understand</strong> and act as a natural way to modularize code. When a large code base is composed of many small Objects, it is <strong>easier to maintain</strong> and change.</p>
</section>
<section>
<h3>OOP & Easy Understanding</h3>
<p>Objects often take on names from their domain which makes them easy to conceptualize.</p>
<br />
<p class="fragment">For example, without seeing my code, you can probably guess what an object called 'Bike' can do (or not do).</p>
</section>
<section>
<h3>OOP & Encapsulation</h3>
<p>One thing that makes code easier to maintain is Encapsulation which is one of the fundamentals of OOP.</p>
<br />
<p class="fragment"><strong>Encapsulation</strong> is a nice way of saying 'put all my properties/behaviors in my capsule where other objects can't touch them'.</p>
<p class="fragment">By assigning certain responsibilities to objects, code is more purposeful and less likely to be changed by accident.
</p>
</section>
<section>
<h3>OOP & Inheritance</h3>
<p>Just as you inherited traits from your parents, your objects may inherit traits from other objects.</p>
<br />
<p class="fragment"><strong>Inheritance</strong> is when you use one class as the basis for another.</p>
</section>
<section>
<h3>OOP & Inheritance</h3>
<p>You might use Inheritance if two objects have the same properties and methods. This way you don't need to write the same thing twice.</p>
<pre><code contenteditable class="fragment ruby">
class MyString < String
end
my_string = MyString.new
my_string.upcase
</code></pre>
</section>
<section>
<h3>Questions?</h3>
</section>
<section>
<h3>Homework</h3>
<p><strong>Practice:</strong> Starting with the first "English Number" excercise, do the exercises at the end of <a href="http://pine.fm/LearnToProgram/?Chapter=08" alt="Chp 8 of Learn to Program">Chapter 8</a> of Learn to Program.</p>
<p><strong>Prep:</strong> Read <a href="http://pine.fm/LearnToProgram/?Chapter=09" alt="Chp 9 of Learn to Program">Chapter 9</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 themes are in /css/theme
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>