forked from flavorjones/workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruby_intro-notes.html
291 lines (199 loc) · 5.16 KB
/
ruby_intro-notes.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
<p><img src="img/ruby-logo.jpg" width="250"></p>
<table width="100%">
<tr>
<td align="center">
<img src="img/ruby-logo.jpg" width="250">
</td>
<td align="center">
<img src="img/rails_logo.jpg">
</td>
</tr>
<tr>
<td align="center">
<span class="big-text">Language</span>
</td>
<td align="center">
<span class="big-text">Framework</span>
</td>
</tr>
</table>
<h1> </h1>
<h1> </h1>
<h2>Matz (Yukihiro Matsumoto)</h2>
<h4>People want to express themselves when they program.</h4>
<h4> </h4>
<h4>They don't want to fight with the language.</h4>
<h4> </h4>
<h4>Programming languages must feel natural to programmers.</h4>
<h4> </h4>
<h4>I tried to make people enjoy programming and concentrate on the fun and creative part of programming when they use Ruby.</h4>
<h3>Ruby 1.0 released in 1996</h3>
<h3>open source</h3>
<h2>Ruby Language Overview</h2>
<ul>
<li>Dynamically typed</li>
<li>Interpreted</li>
<li>Can be modified at runtime</li>
<li>Object oriented</li>
<li>Blocks & lambdas</li>
<li>Perl-like Regular Expressions</li>
</ul>
<p>Let's get started</p>
<p>IRB: Interactive RuBy</p>
<pre><code>>> 4
>> 4 + 4
</code></pre>
<p>Everything is an object</p>
<pre><code>“test”.upcase
“test”.class
“test”.methods
</code></pre>
<p>Everything evaluates to something</p>
<pre><code>2 + 2
(2+2).zero?
</code></pre>
<p>Methods are messages</p>
<pre><code>thing.do(4)
thing.do 4
thing.send "do", 4
</code></pre>
<p>Operators are Methods</p>
<pre><code>1 + 2
1.+(2)
1.send "+", 2
# is a comment
</code></pre>
<h4>Ruby aims to be elegant and readable</h4>
<h4>so punctuation and boilerplate are minimal</h4>
<p>You don't need semicolons</p>
<p>Use parens when you need them</p>
<pre><code>>> "Hello".gsub 'H', 'h'
=> "hello"
>> "Hello".gsub("H", "h").reverse
=> "olleh"
</code></pre>
<p>Variables, symbols, constants</p>
<h2>:this<em>is</em>a_symbol</h2>
<p>There is only one representation of a given symbol in memory, so it really
means "the thing named :this<em>is</em>a_symbol" to the ruby interpreter.
In ruby,
we prefer symbols over hardcoded globals or strings. They're very lightweight.</p>
<p>Collections</p>
<p>Arrays are sized dynamically and can be of mixed types.</p>
<pre><code>a = [1, 2, 3]
a.push "four" #=> [1, 2, 3, "four"]
a.pop #=> "four"
a[0] #=> 1
</code></pre>
<p>Hashes are like an associative map</p>
<pre><code>states = {"MA" => "Massachusetts", "CA" => "California"}
states["MA"]
my_hash = {:a_symbol => 3, "a string" => 4}
my_hash[:a_symbol]
=> 3
</code></pre>
<p>String interpolation</p>
<p>"string #{ruby code} string"</p>
<pre><code>>> a = "world"
>> puts "hello #{a}"
hello world
>> a = 2
>> puts "hello #{a}"
hello 2
>> a = nil
>> puts "hello #{a}"
hello
</code></pre>
<p>Iteration</p>
<pre><code>my_array = ["cat", "dog", ”world"]
my_array.each do |item|
puts "hello " + item
end
my_hash = { :type => "cat",
:name => "Beckett",
:breed => "alley cat" }
my_hash.each do |key, value|
puts "My " + key.to_s + " is " + value
end
</code></pre>
<p>Classes and methods</p>
<pre><code>class Thing
def return_something
"something"
end
end
class Thing
def do_something(a,b)
a + b
end
end
</code></pre>
<p>
var
@var
@@var
$var
VAR</p>
<p>
var # could be a local variable
@var # instance variable
@@var # class variable
$var # global variable
VAR # constant</p>
<p>Methods can take blocks, which are like anonymous functions.</p>
<pre><code>my_array = ["cat", "dog", ”world"]
my_array.each do |item|
puts "hello " + item
end
</code></pre>
<p>def do_something
yield
end</p>
<pre><code>[1, 2, 3].each do |item|
puts "#{item} is #{item.even? ? "even" : "odd"}."
end
</code></pre>
<p>Blocks can also return a value. Map translates each item in an array.</p>
<pre><code>>> ["hello", "world"].map{ |string| string.upcase }
=> ["HELLO", "WORLD"]
</code></pre>
<h4>more neat things about ruby</h4>
<h4>duck typing</h4>
<pre><code>def print_even_or_odd(array_like_thing)
array_like_thing.each do |item|
puts "#{item} is #{item.even? ? "even" : "odd"}."
end
end
print_even_or_odd [1, 2, 3]
print_even_or_odd 1..3
</code></pre>
<h4>advanced ruby</h4>
<h4>meta-programming</h4>
<h4>creating Domain-Specific Languages (DSLs)</h4>
<h1>Rails</h1>
<h1>Rake</h1>
<h1>Cucumber</h1>
<h1>Rspec</h1>
<h1>etc</h1>
<pre><code>method_missing
</code></pre>
<h4>private vs public</h4>
<h4>Private really just means "please don't come in."</h4>
<h4> </h4>
<h4>If someone has access to your runtime environment, they are trusted.</h4>
<h4> </h4>
<h4>Spend your time writing code (and testing it), not protecting yourself from other programmers.</h4>
<pre><code>class Fixnum
def even?
self % 2 == 0
end
end
1.even? #=> false
</code></pre>
<h4>everything is an object</h4>
<h4>Classes are objects</h4>
<h4> </h4>
<h4>class methods are really just methods on the class object.</h4>
<h4> </h4>
<h4>Code evaluated in the scope of a class definition acts on the class object.</h4>
<p>Have fun!</p>