Skip to content

Commit a2c92d8

Browse files
committed
Merge pull request railsbridge#140 from tjgrathwell/ruby_for_beginners_site
Stepfile-style ruby only curriculum
2 parents 924c3ed + a03c7b1 commit a2c92d8

15 files changed

+780
-1
lines changed

lib/step.rb

+43
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ class Step < Erector::Widget
9494
.deploying>h1 {
9595
background-color: #B0DEE7;
9696
}
97+
.further-reading>h1 {
98+
background-color: rgb(135, 229, 242);
99+
}
100+
101+
table.bordered td,
102+
table.bordered tr {
103+
border-style: solid;
104+
border-width: 1px;
105+
border-color: black;
106+
padding-left: 5px;
107+
padding-right: 5px;
108+
}
97109
98110
.requirements>h1 {
99111
background-color: #93B5DA;
@@ -135,6 +147,13 @@ class Step < Erector::Widget
135147
}
136148
}
137149
150+
.in_file > pre {
151+
border: 4px solid #dde;
152+
@include border-radius(4px);
153+
background-color: gray;
154+
color: white;
155+
}
156+
138157
.result > pre {
139158
border: 4px solid #dde;
140159
@include border-radius(4px);
@@ -321,6 +340,7 @@ def todo todo_text
321340
## special
322341

323342
TERMINAL_CAPTION = "Type this in the terminal:"
343+
IRB_CAPTION = "Type this in irb:"
324344
RESULT_CAPTION = "Expected result:"
325345
FUZZY_RESULT_CAPTION = "Approximate expected result:"
326346

@@ -330,6 +350,29 @@ def console msg
330350
pre msg
331351
end
332352
end
353+
354+
def irb msg
355+
div :class => "console" do
356+
span IRB_CAPTION
357+
pre msg
358+
end
359+
end
360+
361+
def type_in_file filename, msg
362+
div do
363+
span "Type this in the file #{filename}:"
364+
source_code :ruby, msg
365+
end
366+
end
367+
368+
def further_reading
369+
div :class => "further-reading" do
370+
h1 "Further Reading"
371+
blockquote do
372+
yield
373+
end
374+
end
375+
end
333376

334377
def result text
335378
div :class => "result" do

sites/ruby/arrays.step

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
goals do
2+
goal "Make some arrays and do stuff with them"
3+
goal "Retrieve data from arrays"
4+
end
5+
6+
step do
7+
irb <<IRB
8+
fruits = ["kiwi", "strawberry", "plum"]
9+
IRB
10+
message 'An array is a list of things in square brackets, separated by commas.'
11+
message 'We generally call the individual things in an array **elements**.'
12+
irb <<-IRB
13+
things = [5, 'tree', 19.5]
14+
things.length
15+
IRB
16+
message 'An array can contain all sorts of things, not just strings.'
17+
message '`length` is a method that tells you how many **elements** are in an array.'
18+
irb <<-IRB
19+
empty_array = []
20+
empty_array.length
21+
IRB
22+
message 'The simplest kind of array is the empty array.'
23+
end
24+
25+
step do
26+
irb <<IRB
27+
fruits[0]
28+
fruits[1]
29+
fruits[2]
30+
IRB
31+
message 'Array elements are stored in order. You can retrieve them by using the square brackets to access them by their **index**.'
32+
message 'Arrays are ordered: elements remain in the same order they started in.'
33+
message 'Ruby starts counting at zero: the first element is `fruits[0]`.'
34+
irb <<IRB
35+
fruits.first
36+
fruits.last
37+
IRB
38+
message 'Ruby gives us some helpful ways to get the first and last element from an array.'
39+
end
40+
41+
step do
42+
irb <<IRB
43+
['salt'] + ['pepper']
44+
IRB
45+
message 'Arrays can be added together with the plus operator.'
46+
irb <<IRB
47+
fruits + ['mango']
48+
fruits
49+
IRB
50+
message 'The plus operator doesn\'t modify the existing array, it makes a new one. How could you write that last piece of code to also modify the fruits array?'
51+
end
52+
53+
step do
54+
irb <<IRB
55+
fruits = ["kiwi", "strawberry", "plum"]
56+
fruits.push('apple')
57+
fruits.pop()
58+
IRB
59+
message 'Ruby has many methods for modifying arrays. What did these two methods do?'
60+
end
61+
62+
explanation do
63+
message "Arrays are used whenever you need to work with a large group of similar items."
64+
message 'A short list of methods for Array:'
65+
table class: 'bordered' do
66+
tr do
67+
td 'length'
68+
td 'how long is this array (how many elements)'
69+
end
70+
tr do
71+
td 'first'
72+
td 'get the first element of the array (same as array[0])'
73+
end
74+
tr do
75+
td 'last'
76+
td 'get the last element of the array (same as array[-1])'
77+
end
78+
tr do
79+
td 'push'
80+
td 'add a new element to the end of the array'
81+
end
82+
tr do
83+
td 'pop'
84+
td 'remove (and return) the element at the end of the array'
85+
end
86+
end
87+
end
88+
89+
further_reading do
90+
a "Ruby's documentation for Array", href: 'http://www.ruby-doc.org/core-1.9.3/Array.html'
91+
end
92+
93+
next_step "hashes"

sites/ruby/command_line.step

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
goals do
2+
goal "Learn about the command line"
3+
goal "Make a new directory"
4+
goal "Browse some directories on your computer"
5+
goal do
6+
rawtext md2html("Learn the commands `pwd`, `mkdir`, `ls`, `cd` and `man`")
7+
end
8+
end
9+
10+
step do
11+
message 'Open up a command line application on your computer. On Macs, this program is called Terminal. On Windows, you might want to use the "Command Line With Ruby and Rails" shortcut that came with RailsInstaller.'
12+
console "pwd"
13+
message '`pwd` means "print working directory". It shows you what **directory** you\'re currently in. Directories are also often called **folders**.'
14+
message '`pwd` can help orient you in the command line if you get lost somewhere in your computer.'
15+
end
16+
17+
step do
18+
irb 'mkdir railsbridge_ruby'
19+
message '`mkdir` means **m**ake **d**irectory. You use mkdir when you want to create a new directory.'
20+
message 'The command line is just one way of manipulating the files on your computer. Try to find the new directory you created in Finder or Windows Explorer.'
21+
message 'If you get an error saying the directory already exists, maybe someone did these steps on your computer before. Don\'t fret.'
22+
end
23+
24+
step do
25+
console 'cd railsbridge_ruby'
26+
message '`cd` means **c**hange **d**irectory. You use cd when you want to move from the current directory into some other directory.'
27+
end
28+
29+
step do
30+
message "If you're on a Mac or Linux computer:"
31+
console 'cd ~'
32+
message "`~` indicates your **home directory**, a directory owned by the account currently logged in to the computer. Your home directory might be something like `/home/sparklepants` (Linux) or `/Users/saucyfrank` (Mac)"
33+
message "Practice moving in and out of various directories. Remember that you can always get back to your home with `cd ~`."
34+
end
35+
36+
step do
37+
console 'ls'
38+
message '`ls` stands for **list**, and shows the contents of the current directory.'
39+
message 'Since you moved back to your Home directory, you should see the newly-created railsbridge_ruby directory in the output of `ls`.'
40+
end
41+
42+
step do
43+
console 'man ls'
44+
message '`man` stands for **manual** and shows you the documentation for a command. This can include various options used to run the command in different ways.'
45+
message "If you see a colon at the bottom of your terminal when viewing a man page, it means you're in a **pager**. Pagers are special programs for showing text that spans multiple pages. You can press the up and down arrows to page through the text, or type `q` to exit."
46+
end
47+
48+
explanation do
49+
message "The command line is an essential tool for computer programmers. While daunting at first, it offers great flexibility in moving around your computer and manipulating files."
50+
message "There are many, many, many more commands available on the command line than what we've seen here, but these are enough to get you going."
51+
message 'Command summary:'
52+
table class: 'bordered' do
53+
tr do
54+
td 'pwd'
55+
td 'print working directory'
56+
td 'print the full path to your current directory'
57+
end
58+
tr do
59+
td 'ls'
60+
td 'list directory'
61+
td 'display the contents of the current directory'
62+
end
63+
tr do
64+
td 'cd [directory]'
65+
td 'change directory'
66+
td 'make this directory the current directory'
67+
end
68+
tr do
69+
td 'man [cmd]'
70+
td 'manual'
71+
td "show the manual for this command. press 'q' to quit."
72+
end
73+
end
74+
end
75+
76+
next_step "irb"

sites/ruby/conditionals.step

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
goals do
2+
goal do
3+
rawtext(md2html('Use `gets` to get input from the user of your program.'))
4+
end
5+
goal 'Use a conditional statement to execute a branch of code only some of the time.'
6+
end
7+
8+
step do
9+
message 'Create a new file called conditionals_test.rb'
10+
type_in_file 'conditionals_test.rb', <<-'CONTENTS'
11+
print "How many apples do you have? > "
12+
apple_count = gets.to_i
13+
puts "You have #{apple_count} apples."
14+
CONTENTS
15+
console 'ruby conditionals_test.rb'
16+
message 'When prompted, type in a number of apples and press enter.'
17+
message "`print` is like `puts` but doesn't make a new line after printing."
18+
message "`gets`, or **get** **s**tring, pauses your program and waits for the user to type something and hit the enter key. It then returns the value back to your program and continues execution. Since the user could have typed anything ('banana', '19.2', '<<!!@@') we use to_i to ensure what comes out is an integer. If the user didn't type a valid integer, `to_i` returns `0`."
19+
end
20+
21+
step do
22+
message 'Continuing on from the end of conditionals_test.rb...'
23+
type_in_file 'conditionals_test.rb', <<-'CONTENTS'
24+
if apple_count > 5
25+
puts "Lots of apples!"
26+
else
27+
puts 'Not many apples...'
28+
end
29+
CONTENTS
30+
console 'ruby conditionals_test.rb'
31+
message 'The `if ... else ... end` construct is a way of changing which lines of your program get executed depending on your data.'
32+
message 'Try running the program with different values for apple_count to see each side of the conditional get executed.'
33+
end
34+
35+
step do
36+
message 'What goes after the `if` is any expression that returns a **boolean**, (the values `true` or `false`). Here\'s some more expressions that return `true` or `false`:'
37+
irb <<-IRB
38+
15 < 5
39+
10 == 12
40+
'foo' != 'bar'
41+
IRB
42+
irb <<-IRB
43+
'sandwich'.end_with?('h')
44+
'sandwich'.end_with?('z')
45+
[1,2,3].include?(2)
46+
[1,2,3].include?(9)
47+
IRB
48+
message 'Many methods return `true` or `false` as well. By convention, methods in Ruby that return booleans end with a question mark.'
49+
end
50+
51+
step do
52+
message 'You can nest a conditional in a loop, as well.'
53+
message 'Create a new file called conditional_loops.rb'
54+
type_in_file 'conditional_loops.rb', <<-'CONTENTS'
55+
fruits = ['apple', 'pear', 'apricot']
56+
fruits.each do |fruit|
57+
if fruit.start_with?('a')
58+
puts "#{fruit} begins with the letter a."
59+
end
60+
end
61+
CONTENTS
62+
console 'ruby conditional_loops.rb'
63+
message "Try changing this conditional so it only prints fruits with at least five letters in their name. Remember to change the string you're `puts`ing as well!"
64+
end
65+
66+
step do
67+
message 'Create a new file called while_loop.rb'
68+
type_in_file 'while_loop.rb', <<-'CONTENTS'
69+
total = 0
70+
user_input = nil
71+
while user_input != 'stop'
72+
print 'Enter a number to add to the total. > '
73+
user_input = gets.chomp
74+
total = total + user_input.to_i
75+
end
76+
puts "Your final total was #{total}!"
77+
CONTENTS
78+
console 'ruby while_loop.rb'
79+
message "A **while** loop continues repeating until a certain statement is false. Here, the program continually asks us for numbers until we say the string 'stop'."
80+
message "It's easy for a while loop to get out of control! If your loop body doesn't do anything to make the **while** condition false, your loop will run forever."
81+
end
82+
83+
explanation do
84+
message "Without some kind of conditional, your program would do the same thing every time. Conditionals let you choose to do different things depending on what data you have in hand."
85+
message 'Now that you know *conditionals*, *loops*, *arrays*, *hashes* and *strings*, you can make some pretty complicated programs!'
86+
end
87+
88+
next_step 'functions'

sites/ruby/functions.step

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
goals do
2+
goal "Create a function"
3+
goal "Call a function"
4+
end
5+
6+
step do
7+
message 'Create a new file called area.rb'
8+
type_in_file 'area.rb', <<-'CONTENTS'
9+
def circle_area(radius)
10+
Math::PI * (radius ** 2)
11+
end
12+
13+
print "What is the radius of your circle? > "
14+
radius = gets.to_i
15+
16+
puts "Your circle has an area of #{circle_area(radius)}"
17+
CONTENTS
18+
console 'ruby area.rb'
19+
message 'When prompted, type in a radius for your circle.'
20+
end
21+
22+
explanation do
23+
message "As your programs get more and more complicated, you'll want to group code into **functions** that can be called over and over again."
24+
message "A **function** begins with **def**, followed by the function name. Next comes any **variables** it takes, followed by the actual body of the function. Lastly, function definitions are finished with the **end** keyword."
25+
message "Pedantic Programmers might want to tell you that Ruby doesn't technically have **functions**, and everything in ruby is really a **method**. It is appropriate to slap these people."
26+
end
27+
28+
next_step 'objects'

0 commit comments

Comments
 (0)