forked from cherimarie/gdi-ruby
-
Notifications
You must be signed in to change notification settings - Fork 1
/
class3.html
452 lines (401 loc) · 17.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
<!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">
<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">
<h3>Intro to Ruby</h3>
<h4>Class 3</h4>
</section>
<section>
<h3>Welcome Back!</h3>
<div class>
<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>
<section>
<h3>Review</h3>
<ul>
<li>Conditions - if, elsif, else</li>
<li>Loops - while, times, each</li>
<li>Arrays - [1,2,3]</li>
</ul>
</section>
<section>
<h3>What we will cover today</h3>
<ul>
<li>Hashes</li>
<li>Methods</li>
<li>Objects</li>
<li>Object Oriented Programming</li>
</ul>
</section>
<!-- 15 minutes-->
<section>
<h3>Hashes</h3>
<p>Hashes are indexed collections like arrays. Each value in a hash actually has two elements, a <strong>key</strong> and a <strong>value</strong>. They can be filled with any data type: integers, floats, strings, even arrays and hashes. </p>
<pre><code class="ruby">
grades_hash = {
"Heather" => 10,
"Tasha" => 8
}
new_hash = {
1 => "number one!",
"num_one" => 12,
"num_two" => 35
}
# some methods to find information about hashes
new_hash.length
grades_hash.has_key?("Tasha")
new_hash.has_value?(35)
</code></pre>
</section>
<section>
<h3>Symbols as Hash Keys</h3>
<p><strong>Symbols</strong> are often used as keys of hashes.</p>
<p>Think of them as another Ruby object that has its own methods.</p>
<pre><code class="ruby">
food = {
"apple" => "a fruit",
"carrot" => "a vegetable"
}
food = {
:apple => "a fruit",
:carrot => "a vegetable"
}
# another way to use symbols in hashes!
# notice the hash rockets can be removed in this case
food = {
apple: "a fruit",
carrot: "a vegetable"
}
</code></pre>
</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 hold any type of objects, and are unordered, we must access values by their <strong>key</strong>.</p>
<pre><code class="ruby">
grades_hash = { "Tasha" => 12, "Joe" => 10, "Heather" => 8 }
new_hash = { 1 => "a number!", "num_one" => 12, "num_two" => 35 }
food_hash = { apple: "a fruit", carrot: "a vegetable" }
grades_hash["Joe"] # returns 10, the value of this key
grades_hash.first # returns first key/value pair... probably
new_hash["num_one"] # returns 12, the value of this key
food_hash[:carrot] # accessing hash with symbols as keys
</code></pre>
</section>
<section>
<h3>Adding & 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 keys and values switched
</code></pre>
<p>Learn more about hashes <a href="https://ruby-doc.org/core-2.5.0/Hash.html" alt="Ruby Docs">here</a>.</p>
</section>
<section>
<h3>Methods</h3>
<p>A <strong>method</strong> is a name for a chunk of code that we can use to perform a specific task.</p>
<p>Methods are often used to define reusable behavior.</p>
<p>Let's look at a couple of methods we've already used:</p>
<pre><code contenteditable class="ruby">
1.even?
=> false
"jordan".capitalize
=> "Jordan"
</code></pre>
<p>If you have some code you want to reuse or organize, methods will help.</p>
</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>num_one, num_two</strong> are parameters</p>
<p><strong>num_one - num_two</strong> is the body of the method</p>
<p><strong>num_one - num_two</strong> is also the return value</p>
<pre><code contenteditable class="ruby">
def subtract(num_one, num_two)
num_one - num_two
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(num_one, num_two)
num_one - num_two
'another thing'
end
return_value = subtract(5, 2)
puts return_value
def subtract(num_one, num_two)
return num_one - num_two
'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 num_one is a parameter</p>
<pre><code contenteditable class="ruby">
def subtract(num_one, num_two)
num_one - num_two
end
subtract(5, 2)
</code></pre>
</section>
<section>
<h3>Splat Arguments</h3>
<pre><code contenteditable class="ruby">
def greet(greeting, *names)
# names is now an array we can iterate through!
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 Parameters</h3>
<p>We can set our method parameters to have a default value if no arguments are passed to it.</p>
<pre><code contenteditable class="ruby">
def eat(food = "chicken")
puts "Yum, #{food}!"
end
eat
Yum, chicken!
eat("kale")
Yum, kale!
</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] || "wheat"
creamer = options[:creamer] || "cream"
puts "Baking a nice #{name} loaf with #{flour} flour and #{creamer}"
end
bake("Sourdough")
bake("Gluten-Free", flour: "rice", creamer: "milk")
bake("Pumpernickel", creamer: "butter")
</code></pre>
</section>
<section>
<h3>Objects</h3>
<p>An <strong>Object</strong> is the building block of a program.</p>
<p>Objects <strong>do things</strong> and have <strong>properties</strong>.</p><br>
<h5>Object: Number</h5>
<p>What can it do?</p>
<p><strong>add, subtract, multiply, divide</strong></p><br>
<p>What are some properties of your Number?</p>
<p><strong>odd, even</strong></p>
</section>
<section>
<h3>Object Class</h3>
<p><strong>Objects</strong> can be grouped by Class.</p>
<p>Classes define a template for objects. They describe what a certain type of object can do and what
properties they have in common.</p>
<p>There are many kinds of objects, including String, Number, Array, Hash, Time...</p>
<p>To create an object we use <strong>.new</strong></p>
<pre><code contenteditable class="ruby">
array = Array.new
</code></pre>
<p>'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="ruby">
# Object is the default root of all other Ruby objects
# We can create an instance of Object as well!
my_object = Object.new
def my_object.say_hello
puts "Hello, World!"
end
my_object.methods
</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="ruby">
cookie = Object.new
def cookie.add_chocolate_chips(num_chips)
@chips = num_chips
end
def cookie.yummy?
@chips > 100
end
cookie.add_chocolate_chips(500)
cookie.yummy? # => true
</code></pre>
<p>What would happen if we called our <strong>yummy?</strong> method before <strong>add_chocolate_chips</strong>?</p>
</section>
<section>
<h3>Object Oriented Programming</h3>
<ul>
<li>Object Oriented Programming is one of many <strong>programming approaches</strong>: procedural, event-driven, functional,
declarative...
</li>
<li>These days, most programming is done in an OO language: Java, Python, C#, C++.</li>
<li>All this means is that programs are <strong>organized around objects</strong> (data & methods).</li>
<li>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.
</li>
</ul>
</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>For example, without seeing code, you can probably guess what an object called 'Bike' or 'Chair' can do (or not do), and what attributes it might have (or not have).</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>
<p><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>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>
<p><strong>Inheritance</strong> is when you use one class as the basis for another.</p>
<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>
</section>
<section>
<h3>OOP & Polymorphism</h3>
<ul>
<li>Similar to inherited traits and behaviors, there are also polymorphic traits and behaviors.</li>
<li><strong>Polymorphic</strong> means many forms.</li>
<li>Within OOP, this relates to different objects being able to respond to the same message in different ways.</li>
</ul>
</section>
<section>
<h3>Homework</h3>
<p><strong>Practice:</strong> Read <a href="http://pine.fm/LearnToProgram/?Chapter=08" alt="Chp 8 of Learn to Program">Chapter 8</a> of Learn to Program and do some of the exercises at the end.</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>