hackety_hack-lessons 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/content/data_types.md +122 -0
- data/content/datastrucs.md +142 -0
- data/content/programming.md +321 -0
- data/content/ruby.md +332 -0
- data/content/shoes.md +208 -0
- data/content/tour.md +189 -0
- data/hackety_hack-lessons.gemspec +17 -0
- data/lib/hackety_hack/lessons.rb +8 -0
- data/lib/hackety_hack/lessons/version.rb +5 -0
- metadata +59 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Steve Klabnik
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# HacketyHack::Lessons
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'hackety_hack-lessons'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install hackety_hack-lessons
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
# Data Types
|
2
|
+
|
3
|
+
## This is where it starts getting fun!
|
4
|
+
|
5
|
+
### Let's get started
|
6
|
+
|
7
|
+
Awesome! Glad to see you here! You're going to learn some great things in this
|
8
|
+
lesson. Ruby has a few different ways to manipulate data, and you're going to
|
9
|
+
play with them!
|
10
|
+
|
11
|
+
Click the icon like this (on the bottom of the screen) to get started:
|
12
|
+
|
13
|
+

|
14
|
+
|
15
|
+
### Lesson Controls
|
16
|
+
|
17
|
+
Before we move on, Here's a refresher on the controls you can use to move around
|
18
|
+
in the Lesson.
|
19
|
+
|
20
|
+

|
21
|
+
__back__: goes back one page
|
22
|
+
|
23
|
+

|
24
|
+
__continue__: goes to the next page
|
25
|
+
|
26
|
+

|
27
|
+
__menu__: makes it easy to jump around to any lesson
|
28
|
+
|
29
|
+

|
30
|
+
__close__: closes the tutor
|
31
|
+
|
32
|
+
Don't forget! Press this to move to the next part:
|
33
|
+
|
34
|
+

|
35
|
+
|
36
|
+
Have at it!
|
37
|
+
|
38
|
+
## Strings
|
39
|
+
|
40
|
+
### You've got this down!
|
41
|
+
|
42
|
+
_String_s are something you already know about! Let's refresh your memory.
|
43
|
+
|
44
|
+
Basically, Strings let you manipulate a bunch of characters. It's sort of like
|
45
|
+
writing something down: and often, Strings are used for handling input and
|
46
|
+
output. Check it out:
|
47
|
+
|
48
|
+
name = "Steve"
|
49
|
+
alert name
|
50
|
+
|
51
|
+
This should be familliar. If it isn't you may want to go review the Basic Ruby
|
52
|
+
lesson before moving on. Gotta learn the basics before you can understand the
|
53
|
+
hard stuff!
|
54
|
+
|
55
|
+
### Concatawhat?
|
56
|
+
|
57
|
+
Here's a big word for you: __concatenation__. It's a mouthful, but luckily for
|
58
|
+
you, it means something really simple:
|
59
|
+
|
60
|
+
first_name = "Steve"
|
61
|
+
last_name = "Klabnik"
|
62
|
+
alert first_name + last_name
|
63
|
+
|
64
|
+
See what I mean by addition? The _+_ lets us __concatenate__ the two Strings
|
65
|
+
together. The first name goes up front, and the last name goes in the back. Nice
|
66
|
+
and easy.
|
67
|
+
|
68
|
+
### Interpawho?
|
69
|
+
|
70
|
+
Okay, since you did so well with that word, I'm going to throw another one at
|
71
|
+
you, while you're still trying to recover: __interpolation__. It kinda means
|
72
|
+
'put into.' See if this makes sense:
|
73
|
+
|
74
|
+
first_name = "Steve"
|
75
|
+
alert "The first name is #{first_name}".
|
76
|
+
|
77
|
+
Whoah! What's up with that? Try running it, and see what it does.
|
78
|
+
|
79
|
+
### They're like pincers
|
80
|
+
|
81
|
+
Terrible analogy alert: See that { and its partner in crime, }? These two _curly
|
82
|
+
braces_ are like the pincers of some strange species of crab. You can put
|
83
|
+
whatever you want between them, and they hold your info in place in the middle
|
84
|
+
of a string.
|
85
|
+
|
86
|
+
|
87
|
+
Oh, and the # (a _hash_), is a funky hat the crab wears. Or something. I dunno.
|
88
|
+
Point is, you need all three parts, #{} and something in the middle. And that's
|
89
|
+
__interpolation__.
|
90
|
+
|
91
|
+
## Arrays
|
92
|
+
|
93
|
+
### The 411
|
94
|
+
|
95
|
+
### Concatination
|
96
|
+
|
97
|
+
### A short shout-out to Modules
|
98
|
+
|
99
|
+
### Not a treasure map...
|
100
|
+
|
101
|
+
### Gotta collect 'em all!
|
102
|
+
|
103
|
+
## Hashes
|
104
|
+
|
105
|
+
### A slightly different Array
|
106
|
+
|
107
|
+
## Putting them together
|
108
|
+
|
109
|
+
### Arrays of Arrays
|
110
|
+
|
111
|
+
### Arrays of hashes
|
112
|
+
|
113
|
+
### Hashes of hashes
|
114
|
+
|
115
|
+
### Hashes of Arrays
|
116
|
+
|
117
|
+
## Summary
|
118
|
+
|
119
|
+
### Good job!
|
120
|
+
|
121
|
+
Awesome! You should be prepared to play around with all kinds of data now. Keep
|
122
|
+
up all the good work!
|
@@ -0,0 +1,142 @@
|
|
1
|
+
# Beginner Data Structures
|
2
|
+
|
3
|
+
## Hello there!
|
4
|
+
|
5
|
+
### Let's get started
|
6
|
+
|
7
|
+
Welcome to your first lesson about Data Structures! Now that you know how to
|
8
|
+
obtain data with Ruby you're also going to be able to store it in collections.
|
9
|
+
Arrays and Hashes are two key classes in Ruby that are able to help with
|
10
|
+
storing your data! Let's get going!
|
11
|
+
|
12
|
+
Click the icon like this (on the bottom of the screen) to get started:
|
13
|
+
|
14
|
+

|
15
|
+
|
16
|
+
### Lesson Controls
|
17
|
+
|
18
|
+
Before we move on, Here's a refresher on the controls you can use to move around
|
19
|
+
in the Lesson.
|
20
|
+
|
21
|
+

|
22
|
+
__back__: goes back one page
|
23
|
+
|
24
|
+

|
25
|
+
__continue__: goes to the next page
|
26
|
+
|
27
|
+

|
28
|
+
__menu__: makes it easy to jump around to any lesson
|
29
|
+
|
30
|
+

|
31
|
+
__close__: closes the tutor
|
32
|
+
|
33
|
+
Don't forget! Press this to move to the next part:
|
34
|
+
|
35
|
+

|
36
|
+
|
37
|
+
Have at it!
|
38
|
+
|
39
|
+
### What are Arrays?
|
40
|
+
|
41
|
+
An array can be thought of as a dresser where each drawer is a numbered spot.
|
42
|
+
This dresser is a collection of slots where one thing can be in each place.
|
43
|
+
Arrays are used to store data like numbers, strings, or even other Arrays!
|
44
|
+
Ruby makes it really easy to create and store things inside of them. Let's get
|
45
|
+
going!
|
46
|
+
|
47
|
+
### What can we do with them?
|
48
|
+
|
49
|
+
my_array = [1,2,"shoes"]
|
50
|
+
|
51
|
+
Try typing this and press the Run button.
|
52
|
+
|
53
|
+
You created an array with the numbers 1, 2, and the string 'shoes' in it.
|
54
|
+
Things in the array can be gotten by typing the array's variable name
|
55
|
+
(`my_array`) and square brackets (`[]`) with a number inside of the brackets.
|
56
|
+
This number can be though of as the address to that spot in the array. It's
|
57
|
+
good to remember that arrays start at 0 in Ruby.
|
58
|
+
|
59
|
+
alert my_array[0]
|
60
|
+
|
61
|
+
What thing in the array are you going to get back? Type this in and press the
|
62
|
+
'Run' button.
|
63
|
+
|
64
|
+
### Arrays in action!
|
65
|
+
|
66
|
+
When using Arrays we need to know a few things first. Arrays in Ruby can expand
|
67
|
+
to the size that you need them. So if you wanted to put the string 'cat' at
|
68
|
+
spot 99(which would be the 100th item in the array) we could put:
|
69
|
+
|
70
|
+
my_array[99] = "cat"
|
71
|
+
|
72
|
+
If there is nothing in a spot you will have 'nil' filling it.
|
73
|
+
|
74
|
+
If we wanted to print out everything in an array we could do something like
|
75
|
+
this:
|
76
|
+
|
77
|
+
my_new_array = ["cat","dog","mouse"]
|
78
|
+
|
79
|
+
Then we would put:
|
80
|
+
|
81
|
+
my_new_array.each {|animal| alert animal}
|
82
|
+
|
83
|
+
Type this all in and press the 'Run' button.
|
84
|
+
|
85
|
+
### Arrays in even more action!
|
86
|
+
|
87
|
+
We've seen what we can do with arrays, but what other things can they do to help
|
88
|
+
us? What if we had an array of numbers and we wanted to sort it? Try typing
|
89
|
+
this this and running it:
|
90
|
+
|
91
|
+
num_array = [4,3,22,19,100,45]
|
92
|
+
|
93
|
+
alert num_array.sort
|
94
|
+
|
95
|
+
That was really easy to sort it from lowest to highest! What if we want it from
|
96
|
+
highest to lowest though? Type this in next and press the 'Run' button.
|
97
|
+
|
98
|
+
alert num_array.reverse
|
99
|
+
|
100
|
+
The array class has so many methods that you can call upon it. Take a look on
|
101
|
+
the Ruby API: http://www.ruby-doc.org/core/classes/Array.html
|
102
|
+
|
103
|
+
## The Hash
|
104
|
+
|
105
|
+
### What are Hashes?
|
106
|
+
|
107
|
+
Now that we've gotten an introduction to Arrays we can also learn about Hashes!
|
108
|
+
Hashes in other languages are sometimes called Dictionaries. Well, what do they
|
109
|
+
do? Like in a dictionary you are able to look up a word or 'key' which
|
110
|
+
corresponds to a 'value'. You separate the key and value with a hashrocket
|
111
|
+
(=>). Just like Arrays you can access a certain key by typing the hashes
|
112
|
+
variable name and the key in square brackets ([]). Let's try working with a
|
113
|
+
hash!
|
114
|
+
|
115
|
+
my_hash = { "dog" => "puppy", "cat" => "kitten" }
|
116
|
+
|
117
|
+
alert my_hash["dog"]
|
118
|
+
|
119
|
+
Try typing this all in and press the 'Run' button!
|
120
|
+
|
121
|
+
### Working with Hashes
|
122
|
+
|
123
|
+
So what else are we able to do with hashes? Let's try something will help us
|
124
|
+
see if something is in the hash as a key or value The methods `has_key?` and
|
125
|
+
`has_value?` are exactly what we're looking for!
|
126
|
+
|
127
|
+
new_hash = { "1" => "one", "2" => "two"}
|
128
|
+
|
129
|
+
alert new_hash.has_key?("1")
|
130
|
+
|
131
|
+
alert new_hash.has_value?("one")
|
132
|
+
|
133
|
+
Try typing these in and press the 'Run' button for each of the methods!
|
134
|
+
|
135
|
+
### Let's tie these Hashes and Arrays together!
|
136
|
+
|
137
|
+
We've looked at two different data structures that are able to hold data for us
|
138
|
+
and let us access certain parts of the collections. Both Arrays and Hashes are
|
139
|
+
commonly used by programmers and are great to have knowledge about! Both of
|
140
|
+
these data structures have so many methods that can be read about in the Ruby
|
141
|
+
Documentation: http://www.ruby-doc.org/core/. Now with Arrays and Hashes you
|
142
|
+
should be able to keep your data organized and usable!
|
@@ -0,0 +1,321 @@
|
|
1
|
+
# Beginner Programming
|
2
|
+
|
3
|
+
## Hello there!
|
4
|
+
|
5
|
+
### Round One
|
6
|
+
|
7
|
+
So, you'd like to learn how to hack code with the best of 'em, eh? Well, you've
|
8
|
+
come to the right place. This is the very first lesson I have to share with you.
|
9
|
+
It all starts here.
|
10
|
+
|
11
|
+
I want to get you started off on the best possible foot with making programs, so
|
12
|
+
we'll start off by talking a little bit about what programming is, and then
|
13
|
+
we'll write some basic programs to draw fun things on the screen. Sound good?
|
14
|
+
Off we go!
|
15
|
+
|
16
|
+
Click the icon like this (on the bottom of the screen) to get started:
|
17
|
+
|
18
|
+

|
19
|
+
|
20
|
+
### Lesson Controls
|
21
|
+
|
22
|
+
Before we move on, Here's a refresher on the controls you can use to move around
|
23
|
+
in the Lesson.
|
24
|
+
|
25
|
+

|
26
|
+
__back__: goes back one page
|
27
|
+
|
28
|
+

|
29
|
+
__continue__: goes to the next page
|
30
|
+
|
31
|
+

|
32
|
+
__menu__: makes it easy to jump around to any lesson
|
33
|
+
|
34
|
+

|
35
|
+
__close__: closes the tutor
|
36
|
+
|
37
|
+
Don't forget! Press this to move to the next part:
|
38
|
+
|
39
|
+

|
40
|
+
|
41
|
+
Have at it!
|
42
|
+
|
43
|
+
## Let's talk about programming
|
44
|
+
|
45
|
+
### It's all about instructions
|
46
|
+
|
47
|
+
When you get down to it, programming is all about __algorithms__. That's a big
|
48
|
+
fancy word for 'a list of instructions.' Every program is simply a big to-do
|
49
|
+
list of instructions for the computer to follow.
|
50
|
+
|
51
|
+
You can turn almost anything into a list of instructions if you really think
|
52
|
+
about it. Most of the time, you've got so much practice at doing things that you
|
53
|
+
don't even think about these individual steps. You just do them. It's very
|
54
|
+
natural.
|
55
|
+
|
56
|
+
### The computer is simple
|
57
|
+
|
58
|
+
Unfortunately, computers are actually quite simple. This may be contrary to
|
59
|
+
everything you've ever heard, but it's the truth. Even though we compare
|
60
|
+
computers to things like our brains, it's a really poor analogy. What computers
|
61
|
+
are actually really good at is performing simple, boring things over and over
|
62
|
+
again very accurately. They can't think for themselves!
|
63
|
+
|
64
|
+
This is why computers appear to be complex. They blindly follow whatever orders
|
65
|
+
they're given, without any thought about how those instructions are good or bad.
|
66
|
+
It's hard to think in such simple terms!
|
67
|
+
|
68
|
+
### Explain yourself well
|
69
|
+
|
70
|
+
It's important to remember that you have to fully explain yourself to the
|
71
|
+
computer when you're programming. It can't figure out what you're trying to say,
|
72
|
+
so you have to say what you mean!
|
73
|
+
|
74
|
+
This takes some practice, so we're going to start off with some exercises in
|
75
|
+
explaining ourselves in very basic terms. It's almost like trying to explain
|
76
|
+
math to a young child: you have to go slowly, triple check your work, and have
|
77
|
+
some patience when it just doesn't quite get it.
|
78
|
+
|
79
|
+
## Lists of Instructions
|
80
|
+
|
81
|
+
### A to-do list, not a shopping list
|
82
|
+
|
83
|
+
When I say that computers follow lists of instructions, I mean a to-do list, not
|
84
|
+
a shopping list. What I'm trying to say is that these lists have an __order__ to
|
85
|
+
them that the computer follows. It does each step in turn as quickly as it
|
86
|
+
possibly can.
|
87
|
+
|
88
|
+
A shopping list is a different kind of list entirely. You can go to whichever
|
89
|
+
aisle you choose, and as long as you get everything before you leave, you're
|
90
|
+
A-OK. This isn't what the computer does at all.
|
91
|
+
|
92
|
+
### How would you tell a person to do it?
|
93
|
+
|
94
|
+
Let's try an example: if you had to tell someone in words how to draw a square
|
95
|
+
on a piece of paper, how would you do it?
|
96
|
+
|
97
|
+
You're not allowed to say "like this" or "this way," that's cheating! You have
|
98
|
+
to spell out every detail.
|
99
|
+
|
100
|
+
### Once again: computers are simple
|
101
|
+
|
102
|
+
How'd you do? I can't see what you said, but here's an example of how simple
|
103
|
+
computers are compared to people. Did you forget to mention how long each side
|
104
|
+
of the square is? If you didn't good job!
|
105
|
+
|
106
|
+
Here's how I'd do it, by the way. This isn't the only right answer, it's just an
|
107
|
+
example:
|
108
|
+
|
109
|
+
1. Put your pen down on the paper.
|
110
|
+
2. Draw right one inch.
|
111
|
+
3. Draw down one inch.
|
112
|
+
4. Draw left one inch.
|
113
|
+
5. Draw up one inch.
|
114
|
+
6. Take your pen off of the paper.
|
115
|
+
7. Finished!
|
116
|
+
|
117
|
+
## Turtles, all the way down.
|
118
|
+
|
119
|
+
### Drawing... with turtles?
|
120
|
+
|
121
|
+
Okay! Enough of these thinking experiments. Let's actually make something. I bet
|
122
|
+
you've been wondering when that was going to start, right? It's really important
|
123
|
+
to get the basics down first.
|
124
|
+
|
125
|
+
We're going to tell the computer how to draw shapes... with turtles. Yes, you
|
126
|
+
heard me right. You're going to have to give these instructions to a turtle.
|
127
|
+
|
128
|
+
This particular turtle is carrying a pen. You have a piece of paper. The turtle
|
129
|
+
will follow your every word. But the turtle is a bit slow, and needs careful
|
130
|
+
instruction. Are you up to it?
|
131
|
+
|
132
|
+
### The Turtle and its commands
|
133
|
+
|
134
|
+
We have to tell Hackety Hack that we want to tell the Turtle what to do. To do
|
135
|
+
that, we have a `Turtle` command. We can tell the `Turtle` two things:
|
136
|
+
|
137
|
+
`draw`: the turtle will follow our instructions at lightning speed,
|
138
|
+
|
139
|
+
`start`: an interactive window will appear, which lets you see the turtle move
|
140
|
+
at each step in the program. You can move at your own pace. This is useful if
|
141
|
+
the turtle isn't doing what you expect!
|
142
|
+
|
143
|
+
Click on the editor tab to get started:
|
144
|
+
|
145
|
+

|
146
|
+
|
147
|
+
### Type it in!
|
148
|
+
|
149
|
+
Cool. Now type this:
|
150
|
+
|
151
|
+
Turtle.draw
|
152
|
+
|
153
|
+
The period in between the `Turtle` and the `draw` connects them together.
|
154
|
+
Programming languages have rules, just like English has rules! You can think of
|
155
|
+
`Turtle` like a subject, and `draw` as a verb. Together, they make a sentence:
|
156
|
+
hey turtle, draw me something!
|
157
|
+
|
158
|
+
Once you've typed that in, go ahead and click the 'Run' button. The turtle moves
|
159
|
+
so quickly in `draw` mode that you won't even see him, but I assure you, he's
|
160
|
+
there!
|
161
|
+
|
162
|
+
### Do... what I tell you to
|
163
|
+
|
164
|
+
Awesome! We've got the turtle going, at least. Now we need to tell it what we
|
165
|
+
want to draw!
|
166
|
+
|
167
|
+
Remember when we said that all programs are lists of instructions? In this case,
|
168
|
+
our program only has one instruction: `Turtle`, draw something! But we need to
|
169
|
+
be able to give the `Turtle` its own list of instructions.
|
170
|
+
|
171
|
+
To do this, we'll use two words: `do` and `end`. These two words together make
|
172
|
+
up a _sublist_ of things, just for the `Turtle`!
|
173
|
+
|
174
|
+
### Changing the background
|
175
|
+
|
176
|
+
Let's try this: we can tell the `Turtle` that we want to use a different
|
177
|
+
background color by using the `background` command. Check it out:
|
178
|
+
|
179
|
+
Turtle.draw do
|
180
|
+
background maroon
|
181
|
+
end
|
182
|
+
|
183
|
+
Type this in and click 'Run'!
|
184
|
+
|
185
|
+
### The Turtle gets its orders
|
186
|
+
|
187
|
+
Cool stuff! The background is now maroon. You can find a full list of colors
|
188
|
+
that are supported on the [Shoes
|
189
|
+
website](http://shoesrb.com/manual/Colors.html).
|
190
|
+
|
191
|
+
This is also how you make lists of instructions for the `Turtle` to follow. To
|
192
|
+
make it a little easier to see, programmers will often put two spaces before
|
193
|
+
sublists. Get in the habit now, you'll thank me later!
|
194
|
+
|
195
|
+
### The pen
|
196
|
+
|
197
|
+
Now that we've got a snazzy background color, how do we draw some lines? Well,
|
198
|
+
the first thing you need to know about is the pen. The `Turtle` carries a pen
|
199
|
+
along, and drags it along the ground behind itself. You can change the color of
|
200
|
+
line the pen draws with the `pencolor` command.
|
201
|
+
|
202
|
+
## Drawing lines
|
203
|
+
|
204
|
+
### Sally forth!
|
205
|
+
|
206
|
+
Okay, enough dilly-dallying. Let's tell the turtle to draw a line! Here's my
|
207
|
+
line. Give this one a shot, then try your own colors and numbers!
|
208
|
+
|
209
|
+
Turtle.draw do
|
210
|
+
background lightslategray
|
211
|
+
pencolor honeydew
|
212
|
+
forward 50
|
213
|
+
end
|
214
|
+
|
215
|
+
50 is the number of pixels to move foward, by the way.
|
216
|
+
|
217
|
+
### You spin me right round, baby
|
218
|
+
|
219
|
+
Great! So you've got a line. But what if you don't want the `Turtle` to move
|
220
|
+
forward? Well, you can tell it to turn by using a `turnleft` or `turnright`
|
221
|
+
command, like this:
|
222
|
+
|
223
|
+
Turtle.draw do
|
224
|
+
background lightslategray
|
225
|
+
pencolor honeydew
|
226
|
+
forward 50
|
227
|
+
turnright 90
|
228
|
+
forward 50
|
229
|
+
end
|
230
|
+
|
231
|
+
Give that a shot, then play with it a bit!
|
232
|
+
|
233
|
+
If you're wondering what 90 means, it's the number of degrees that it'll turn.
|
234
|
+
|
235
|
+
### I like to move it, move it
|
236
|
+
|
237
|
+
Okay, now we're cooking! Let's break this down again:
|
238
|
+
|
239
|
+
`Turtle.draw` tells the `Turtle` we want it to draw some things. The period
|
240
|
+
connects the two.
|
241
|
+
|
242
|
+
`do ... end` is a sublist of things. This is what we want the `Turtle` to be
|
243
|
+
drawing. Not for the rest of our program.
|
244
|
+
|
245
|
+
`pencolor` sets the color of the pen the `Turtle` is dragging along behind him,
|
246
|
+
and `background` sets the color of the background.
|
247
|
+
|
248
|
+
`turnright` (or its buddy `turnleft`) tells the `Turtle` to turn to the right or
|
249
|
+
left.
|
250
|
+
|
251
|
+
`forward` (or its friend `backward`) tells the `Turtle` to move.
|
252
|
+
|
253
|
+
### Let's try drawing that square
|
254
|
+
|
255
|
+
Go ahead. Give it a shot. Try to get the `Turtle` to draw a square.
|
256
|
+
|
257
|
+
I'll wait. :)
|
258
|
+
|
259
|
+
### Here's my version
|
260
|
+
|
261
|
+
Here's how I did it:
|
262
|
+
|
263
|
+
Turtle.draw do
|
264
|
+
background lightslategray
|
265
|
+
pencolor honeydew
|
266
|
+
forward 50
|
267
|
+
turnright 90
|
268
|
+
forward 50
|
269
|
+
turnright 90
|
270
|
+
forward 50
|
271
|
+
turnright 90
|
272
|
+
forward 50
|
273
|
+
end
|
274
|
+
|
275
|
+
## Repeating ourselves
|
276
|
+
|
277
|
+
### Pete and Repeat...
|
278
|
+
|
279
|
+
Man, that was a ton of reptition! My fingers almost fell off typing `forward`
|
280
|
+
and `turnright` there!
|
281
|
+
|
282
|
+
I have good news, though: I mentioned something earlier about computers. It
|
283
|
+
turns out that doing boring, repetitive things is something they're really good
|
284
|
+
at! They'll do the same thing over and over again, forever even as long as you
|
285
|
+
ask nicely.
|
286
|
+
|
287
|
+
### Repeating repeating ourselves ourselves
|
288
|
+
|
289
|
+
Check it out: our `Turtle` actually knows numbers. For example:
|
290
|
+
|
291
|
+
Turtle.draw do
|
292
|
+
background lightslategray
|
293
|
+
pencolor honeydew
|
294
|
+
4.times do
|
295
|
+
forward 50
|
296
|
+
turnright 90
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
Try running this example. It also draws a square! Wow!
|
301
|
+
|
302
|
+
### 4.times
|
303
|
+
|
304
|
+
It's pretty easy: `4` can take instructions too, just like our `Turtle`. This
|
305
|
+
command repeats a list of instructions that many times. Fun! Four times. And the
|
306
|
+
`do` and `end` show which list of instructions go with the `4` rather than with
|
307
|
+
the `Turtle`.
|
308
|
+
|
309
|
+
### Try it out!
|
310
|
+
|
311
|
+
Have a blast: make some fun shapes of your own!
|
312
|
+
|
313
|
+
## Summary
|
314
|
+
|
315
|
+
### Congratulations!
|
316
|
+
|
317
|
+
Wow, you're awesome. Pat yourself on the back. High five someone. You've got
|
318
|
+
these basics down!
|
319
|
+
|
320
|
+
Check out the _Basic Ruby_ lesson to pick up some totally different and exciting
|
321
|
+
things!
|