forked from cherimarie/gdi-ruby
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclass3.html
More file actions
430 lines (373 loc) · 19.3 KB
/
Copy pathclass3.html
File metadata and controls
430 lines (373 loc) · 19.3 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Intro to Ruby : Class 3 ~ 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="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/gdiaa.css" id="theme">
<link rel="stylesheet" href="lib/css/light.css">
<link rel="stylesheet" href="css/print/pdf.css" media="print">
<script src="lib/js/head.min.js"></script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<!-- Welcome & Intros - 20 min -->
<section>
<img src="images/gdi_logo_badge.png" class="img--bare" height="450px" />
<div class="box--small">
<h3><span class="green">Intro to Ruby</span></h3>
<h4><span class="blue">Class 3</span></h4>
</div>
</section>
<section>
<h3>Welcome!</h3>
<!-- <p class="box"><strong>Girl Develop It</strong> is here to empower and assist women interested in getting into and advancing in the tech industry through affordable and accessible programs.</p> -->
<p class="box"><strong>Girl Develop It</strong> is here to provide affordable programs for adult women interested in learning web and software development in a judgment-free environment.</p>
<div class="copy--small">
<h4 class="green">Some "rules"</h4>
<ul>
<li>We are here for you!</li>
<li>Every question is important.</li>
<li>Help each other.</li>
<li><strong>Have fun!</strong></li>
</ul>
</div>
<p class="box copy--small"><small><strong>Girl Develop It is dedicated to providing a harrasment free learning experience for <em>everyone</em>.</strong><br/>For more information, see our <a href="http://www.girldevelopit.com/code-of-conduct" target="_blank">Code of Conduct</a>.</small></p>
</section>
<!-- Homework Review - 10 min -->
<section>
<h3>Homework Discussion</h3>
<p class="box ">How was last week's homework?</p>
<p class="box copy--small">Do you have any questions or concepts that you'd like to discuss?</p>
<h6 class="grey">The Homework was:</h6>
<p class="box--small copy--xsmall left-align"><strong>Practice:</strong> Find two interesting methods for Enumerable (via <a href="http://www.ruby-doc.org/core-2.1.1/Enumerable.html" alt="Ruby Docs">Ruby Docs</a>) and write a little program that uses them for arrays or hashes. Do any one of the exercises at the end of <a href="http://pine.fm/LearnToProgram/?Chapter=06" alt="Chp 6 of Learn to Program">Chapter 6</a> of Learn to Program.</p>
<p class="box--small copy--xsmall left-align"><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>
<section>
<h3>Review</h3>
<ul class="box--small list--xtall">
<li class="fragment">Conditions - <code>if</code>, <code>elsif</code>, <code>else</code></li>
<li class="fragment">Loops - <code>while</code>, <code>for</code></li>
<li class="fragment">Arrays - <code>[1,2,3]</code></li>
<li class="fragment">Hashes - <code>{"one" => 1, "two" => 2}</code></li>
</ul>
</section>
<section>
<h3>What we will cover today</h3>
<ul class="list--xtall box--small">
<li class="fragment">Methods</li>
<li class="fragment">Objects</li>
<li class="fragment">Object Oriented Programming</li>
</ul>
</section>
</section>
<!-- Methods -->
<section>
<section>
<h3>Methods</h3>
<p class="box--small copy--small">A <strong>method</strong> is a name for a chunk of code</p>
<p class="box--small copy--small fragment">Methods are often used to define reusable behavior.</p>
<div class="fragment"><p class="copy--xsmall">Let's look at a method we've already used:</p>
<pre style="height: 125px; overflow: hidden"><code contenteditable class=" ruby">
1.even?
=> false
"fred".capitalize
=> "Fred"
</code></pre></div>
<p class="box--small copy--small fragment">If you have code you want to reuse or organize, methods will help.</p>
</section>
<section>
<h3>Let's Develop It!</h3>
<p class="box--small copy--small">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 class="box--small copy--small">Do they have any similar methods?</p>
<pre><code contenteditable class="ruby">
"abc".next
=> "abd"
"abz".next
=> "aca"
1.next
=> 2
-1.next
=> 0
</code></pre>
</section>
<section>
<h3>Write a Method</h3>
<p class="copy--small box--small">Let's write a method together...</p>
<pre style="height: 100px; overflow: hidden"><code contenteditable class="ruby">
def subtract(x, y)
result = x - y
return result
end
</code></pre>
<p class="copy--xsmall box--small"><code>def</code> means define</p>
<p class="copy--xsmall box--small"><code>subtract</code> is the name of the method</p>
<p class="copy--xsmall box--small"><code>x, y</code> are parameters</p>
<p class="copy--xsmall box--small"><code>x - y</code> is the body of the method</p>
<p class="copy--xsmall box--small"><code>x - y</code> is also the return value</p>
</section>
<section>
<h3>Let's Develop It!</h3>
<p class="copy--small box--small">Let's write a method that takes in a number and tells us if it is even.</p>
<p class="copy--small box--small"><strong>Hint:</strong> if <code>number % 2 == 0</code>, 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 class="copy--small box--small">Every Ruby method returns something.</p>
<p class="copy--small box--small">The <code>return</code> keyword indicates what comes back when calling the method</p>
<pre><code contenteditable class="ruby">
def subtract(x,y)
return x - y
end
def subtract_that_lies(x,y)
result = x - y
return "a string literal, not the result of subtraction"
end
return_value = subtract(5, 2)
puts return_value # 3
return_value = subtract_the_lies(5, 2)
puts return_value # "a string literal, not the result of subtraction"
</code></pre>
</section>
<section>
<h3>Arguments vs. Parameters</h3>
<p class="copy--small box--small">Technically speaking, arguments are <i>passed</i><br/>and parameters are <i>declared</i>.</p>
<p class="copy--small box--small">Note that the variable names don't have to match!</p>
<p class="copy--small box--small">In this code, <code>5</code> is an argument and <code>x</code> is a parameter</p>
<pre><code contenteditable class="ruby">
def subtract(x,y)
x - y
end
subtract(5, 2)
</code></pre>
</section>
<section>
<h3>Methods & Variables</h3>
<p class="copy--small box--small"><strong>Local variables</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>
<!-- Objects -->
<section>
<section>
<h3>Objects</h3>
<p class="box ">An <strong>Object</strong> is the building block of a program.</p>
<p class="box ">Objects <strong>do things</strong> and have <strong>properties</strong>.</p>
</section>
<section>
<h5>Object: <code>String</code></h5>
<div class="left" style="width: 48%">
<p class="box--small copy--small">What can it do?</p>
<ul class="list--tall copy--xsmall">
<li> Capitlize: <code>"hello".capitalize</code></li>
<li> Reverse: <code>"hello".reverse</code></li>
<li> Suffixes: <code>"hi".end_with?("i")</code></li>
</ul></div>
<div class="right" style="width: 48%">
<p class="box--small copy--small">What are some properties?</p>
<ul class="list--tall copy--xsmall"><li> Length: <code>"hello".length</code></li>
<li> Empty: <code>"hello".empty?</code></li>
</ul>
</div>
</section>
<section>
<h3>Class Definition</h3>
<p class="copy--small box--small"><strong>Objects</strong> are defined by their <strong>Class</strong></p>
<p class="copy--small box--smallfragment">They are the blueprint for what your object can do, and they're what allow you to make your own custom objects for your programs.</p>
<div class="fragment">
<pre><code contenteditable class=" ruby">
class Person
def initialize(name, birth_year)
@name = name
@birth_year = birth_year
end
def age
Time.now.year - @birth_year
end
def greet
return "Hi, my name is #{@name}, and I am #{self.age} years old"
end
end
</code></pre>
</div>
</section>
<section>
<h3>Object Methods</h3>
<p class="copy--small box--small">Methods can be defined on objects.</p>
<p class="copy--small box--small">They give us some behavior, some chunk of reusable work that live with our objects.</p>
<pre class="fragment "><code contenteditable class="ruby">
class Child
def favorite_food
["Hamburgers", "Waffles", "Apple Pie"].sample
end
end
child = Child.new
child.favorite_food # some random food…
</code></pre>
</section>
<section>
<h3>Instance Variables</h3>
<p class="copy--small box--small">Represent object state, the information or data that lives with our objects</p>
<p class="copy--small box--small">Names start with an <code>@</code></p>
<p class="copy--small box--small">Useable across all of our object's methods</p>
<pre class="fragment"><code contenteditable class=" ruby">
class Cookie
def add_chips(number_of_chips)
@number_of_chips = number_of_chips
end
def yummy?
@number_of_chips > 100
end
end
cookie = Cookie.new
cookie.add_chips(50)
cookie.yummy? # false
cookie.add_chips(200)
cookie.yummy? # true
</code></pre>
</section>
<section>
<h3>Putting It All Together</h3>
<pre><code contenteditable class=" ruby">
class Person
def initialize(name, birth_year)
@name = name
@birth_year = birth_year
end
def age
Time.now.year - @birth_year
end
def greet
return "Hi, my name is #{@name}, and I am #{self.age} years old"
end
end
john = Person.new("John", 1990)
john.greet # "Hi, my name is John, and I am 26 years old"
</code></pre>
</section>
</section>
<!-- OOP -->
<section>
<section>
<h3>Object Oriented Programming</h3>
<ul class="list--tall copy--small box--small">
<li>Object Oriented Programming is one of many <strong>programming approaches</strong>: procedural, event-driven, functional,
declarative...
</li>
<li class="fragment">These days, most programming is done in an OO language: Java, C#, C++.</li>
<li class="fragment">All this means is that programs are <strong>organized around objects</strong> (data & methods).</li>
<li 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.
</li>
</ul>
</section>
<section>
<h3>OOP & Easy Understanding</h3>
<p class="copy--small box">Objects often take on names from their domain which makes them easy to conceptualize.</p>
<p class="copy--small box">For example, without seeing my code, you can probably guess what an object called <code>Bike</code> can do (or not do)</p>
</section>
<section>
<h3>OOP & Encapsulation</h3>
<p class="copy--small box">One thing that makes code easier to maintain is Encapsulation which is one of the fundamentals of OOP.</p>
<p class="copy--small box"><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="copy--small box">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 class="copy--small box--small">Just as you inherited traits from your mom, your objects may inherit traits from other objects.</p>
<p class="copy--small box--small"><strong>Inheritance</strong> is when you use one class as the basis for another.</p>
<div class="fragment"><p class="copy--xsmall box--small">You might use Inheritance if two objects have the same properties and methods.<br/>This way you don't need to write the same thing twice.
</p>
<pre><code contenteditable class=" ruby">
class Person
def initialize(name, birth_year)
@name = name
@birth_year = birth_year
end
def age
Time.now.year - @birth_year
end
end
class Actor < Person
def act_your_heart_out
puts "I am acting! \o/"
end
end
</code></pre></div>
</section>
</section>
<section>
<section>
<h3>Questions?</h3>
</section>
<section>
<h3>Homework</h3>
<p class="left-align copy--small box"><strong>Practice:</strong> Do some of 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 class="left-align copy--small box"><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>
<section>
<img src="images/gdi_logo_badge.png" class="img--bare" height="450px" />
<div class="box--small">
<h3><span class="green">Intro to Ruby</span></h3>
<h4><span class="blue">Class 3</span></h4>
</div>
</section>
</section>
</div>
<footer>
<div class="copyright">
Intro to Ruby ~ GDI Ann Arbor ~
<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="js/jquery.min.js"></script>
<script src="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,
rollingLinks: 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: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>