-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathbasic_ruby.rb
More file actions
285 lines (261 loc) · 11.4 KB
/
Copy pathbasic_ruby.rb
File metadata and controls
285 lines (261 loc) · 11.4 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
# encoding: UTF-8
lesson_set "3: Basic Ruby" do
lesson "Hello there!"
page "Let's get started" do
para "Welcome to your first lesson in Ruby! You're going to have a blast."
para "Ruby is a great programming language that you can use to make all kinds of ",
"things with. Let's get going!"
flow do
para "(click the little "
icon_button :arrow_right, nil do
alert "Not this one! The one below!"
end
para " on the bottom of the screen to get started)"
end
end
page "Lesson Controls" do
para "Before we move on, Here's a refresher on the controls you can use ",
"to move around in the Lesson."
flow do
icon_button :arrow_left, nil
para strong("back"), ": goes back one page"
end
flow do
icon_button :arrow_right, nil
para strong("continue"), ": goes to the next page"
end
flow do
icon_button :menu, nil
para strong("menu"), ": makes it easy to jump around to any lesson"
end
flow do
icon_button :x, nil
para strong("close"), ": closes the tutor"
end
para "Don't forget! Press "
icon_button :arrow_right, nil
para "to move to the next part. Have at it!"
end
lesson "A bit more about Ruby"
page "Konnichiwa, Ruby!" do # can't do 日本語 without a bunch of work...
flow do # due to multiple fonts...
para em("Ruby"), " was created by "
para "まつもと ゆきひろ", :font => "TakaoGothic"
para " (you can just call him Matz) in 1995. If you couldn't guess, Matz is ",
"from Japan. Here he is:"
end
image "#{HH::STATIC}/matz.jpg"
end
page "Ruby is enjoyable" do
para "Matz has this to say about Ruby:\n"
para em("I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming, and to be happy. That is the primary purpose of Ruby language.\n")
para "One more thing about Ruby: Rubyists (that's what people who like Ruby call ",
"themselves) have a saying: ", strong("MINSWAN"), ". This stands for ",
strong("M"), "atz ", strong("I"), "s ", strong("N"), "ice ",
strong("S"), "o ", strong("W"), "e ", strong("A"), "re ", strong("N"), "ice. ",
"Which is a pretty nice saying, itself. Be nice to everyone, and give them ",
"a hand when they need it!"
end
lesson "Displaying Things"
page "Let's do this!" do
para "Okay! The very first thing that you need to know is how to show something ",
"on the screen. Otherwise, you won't know what's going on!"
flow do
para "In order to start coding, we need to bring up the Editor. Click the ("
image "#{HH::STATIC}/tab-new.png", :margin => 6 do
alert("Not this one, silly! the one on the left!")
end
para ") to open it up."
end
next_when :tab_opened, :Editor
end
page "Hello, World!" do
para "There are two ways of doing this. Here's the first: alert"
embed_code 'alert "Hello, world!"'
para "Type this in and press the 'Run' button."
end
page "alert" do
para "Okay, let's break this down: There's two main parts to this little program: ",
"you have an ", code("alert"), ", and a ", code('"Hello, world!"'), ". These ",
"two parts work just like an English sentence: The ", code("alert"), " is a ",
'verb and the stuff in the ""s is an object. In Ruby, we call verbs ',
strong("methods"), ". The ", code("alert"), " verb says 'Put an alert box ",
"on the screen, and the content of the box is whatever thing you give me.'"
para "We'll talk about the ", code('"Hello, world!"'), " in just a second. Here's ",
"the other way of making this happen: "
embed_code 'puts "Hello, world!"'
para "But if you try that here, it won't work! The ", code("puts"), " method ",
"doesn't display a dialog box, it puts text out to a command-line prompt. ",
"Since Hackety Hack is all graphical, this doesn't work here. So we'll ",
"be using ", code("alert"), "s throughout these tutorials, but if you look ",
"at other Ruby tutorials, you may see ", code("puts"), "."
end
lesson "Letters, words, and sentences"
page "Strings" do
para "Okay! Now that you've got that verb bit down, it's time to learn about ",
em("String"), "s. Strings are what we call a bunch of words between a pair ",
"of \" characters. The \"s are used to tell the computer what words you ",
"actually want to say. Let's think about our example:"
embed_code 'alert "Hello, world!"'
para "If you didn't have the \"s, the computer wouldn't know which words were ",
"methods and which ones were part of the string! And consider this:"
embed_code 'alert "I am on high alert!"', :run_button => true
para "Without making all of those words a string, how would Ruby know that the ",
"second alert was some text you wanted to say, rather than another alert box?"
end
page "Adding Strings" do
para "Now, if you want to put two bits of strings together, you can use the ",
code("+"), " character to do it. Try typing this:"
embed_code 'alert "Hello, " + "world!"'
para "Same thing! The ", code("+"), " sticks the two strings together. This ",
"will end up being super useful later!"
end
lesson "Numbers and Math"
page "Numbers" do
para "You can just use numbers, and Ruby understands them:"
embed_code "alert 2"
para "You can even use numbers that have a decimal point in them:"
embed_code "alert 1.5"
end
page "Basic Math" do
para "You can also do math with numbers, and it'll work out pretty well:"
embed_code "alert 1 + 2"
para ""
embed_code "alert 5 - 3"
para ""
embed_code "alert 2 * 3"
para ""
embed_code "alert 4 / 2"
para ""
para "But if you try this, nothing happens:"
embed_code 'alert "hey" + 2'
para "This is kind of fun and silly, though:"
embed_code 'alert "hey" * 2'
end
page "Errors" do
para "You know how nothing happened when you hit the Run button earlier? That ",
"was because there was an error. You can see any errors that run by hitting ",
"either Alt-/ or Command-/, depending on what kind of computer you're using."
para "The error that results from ", code('alert "hey" + 2'), " is "
embed_code "can't convert Fixnum into String"
para "What is that?"
end
lesson "A few words about types"
page "Why's it do that?" do
para "Each part of a Ruby program is an ", code("Object"), ". Right now, all you ",
"need to know about ", code("Object"), "s is that it's sort of like saying ",
'"a thing." Your program is made up of a bunch of ', code("Object"), "s ",
"working together."
para "We'll learn more about ", code("Object"), "s in a future lesson, but there ",
"is one thing I'll tell you: ", code("Object"), "s have a 'type.' This lets ",
"Ruby know what kind of ", code("Object"), " it is."
end
page "Adding numbers to words" do
para "That's why"
embed_code 'alert "hey" + 2'
para 'doesn\'t really work: "hey" is a ', code("String"), " object, and 2 is a ",
code("Fixnum"), " object. And adding ", code("String"), "s and ",
code("Fixnum"), "s doesn't make any sense. We can make this code work, though!"
para "All we need to do is turn the ", code("Fixnum"), " into a ", code("String"),
". We can do this by using the ", code("to_s"), " method."
embed_code 'alert "hey" + 2.to_s'
end
page "Let's look at that again" do
embed_code 'alert "hey" + 2.to_s'
para "Okay, this isn't bad. We have our ", code("alert"), " method. We're giving it ",
code('"hey" + 2.to_s'), ". The ", code("2.to_s"), " turns a ",
code("Fixnum"), " 2, which is like the mathematical idea of a 2, into the ",
code("String"), " 2, which is like when you write a 2 down on a piece of ",
"paper."
end
lesson "Variables"
page "They're like boxes" do
para "What happens if we want to keep something around? Most programs are not ",
"one line, I assure you. You can use a ", em("variable"), " to hold a ",
"value and use it later. It's like a box that you put things in."
para "Let's try one out:"
embed_code 'message = "Hello, world!"
alert message'
para "Give that a run."
end
page "Assignment" do
para "Cool stuff! We used an ", code("="), " to ", em("assign"), " the ", code("String"), '"Hello, world!" into the variable ', code("message"), ". We then passed that ",
code("message"), " to the ", code("alert"), " method."
para "As you can see, we can use variables in place of another value. Try this:"
embed_code 'number = 5
number = number * 2
number = number - 8
number = number + 1
alert number'
para "Make a guess before you run this program."
end
lesson "User Input"
page "ask-ing for it." do
para "We can ask the user of our program for some input, and then put their answer ",
"into a variable. It's easy! Check this program out:"
embed_code 'name = ask "What is your name?"
alert "Hello, " + name'
para "The ", code("ask"), " method brings up a box and lets our users type ",
"something in. Fun! We put their answer into the ", code("name"), " variable ",
"and then showed it with ", code("alert"), ". Sweet!"
end
lesson "Basic flow control"
page "if..." do
para "Remember back to that Beginning Programming lesson... we talked about how ",
"programs are one big list, that the computer follows in order."
para "Well, guess what? We can actually change this order by using certain bits ",
"of code. Compare these two programs:"
embed_code 'number = 2
if number == 2
alert "Yes!"
else
alert "No!"
end'
embed_code 'number = 1
if number == 2
alert "Yes!"
else
alert "No!"
end'
para "There are a few new things here."
end
page "==" do
para "Here it is again:"
embed_code 'number = 2
if number == 2
alert "Yes!"
else
alert "No!"
end'
para "The == command is just a bit different than the = command. == tests the ",
code("Object"), " on its right against the ", code("Object"), " on its left. ",
"If the two are equal, then the code after the ", code("if"), " will run. ",
"If they're not equal, you get the code after the ", code("else"), ". The ",
code("end"), " lets us know we're done with our ", code("if"), "."
end
lesson "Example: a guessing game"
page "Guess!" do
para "Let's put this all together:"
embed_code 'secret_number = 42.to_s
guess = ask "I have a secret number. Take a guess, see if you can figure it out!"
if guess == secret_number
alert "Yes! You guessed right!"
else
alert "Sorry, you\'ll have to try again."
end'
para "Can you guess what ", code("to_s"), " does, and why you need it? If you're ",
"stumped, try asking ", link("on the Hackety Hack site",
:click => "http://hackety-hack.com/stream"), " ",
"and we'll give you a hand."
end
lesson "Summary"
page "Good job!" do
para "Congrats! You've picked up all of the basics of Ruby. There's a lot more ",
"you still have to learn, though!"
para "Here's what you've learned so far:"
para "* ", code("alert"), " and ", code("ask")
para "* ", code("="), ", variables, and ", code("==")
para "* ", code("if"), " and ", code("else")
para "Awesome! You'll want to check out Basic Shoes next!"
end
end