Skip to content

Commit cb039ff

Browse files
committed
Merge branch 'patch-4' of https://github.com/kejadlen/docs into kejadlen-patch-4
2 parents b5869ea + 32627e6 commit cb039ff

File tree

1 file changed

+67
-67
lines changed

1 file changed

+67
-67
lines changed

sites/ruby/loops.step

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,67 @@
1-
goals do
2-
goal "Use loops to do operations for every element in an array."
3-
goal do
4-
rawtext(md2html("Use `puts` to print strings to the screen."))
5-
end
6-
goal 'Learn the two different syntaxes for creating blocks in Ruby.'
7-
end
8-
9-
step do
10-
irb <<-IRB
11-
puts 'Hello World'
12-
IRB
13-
message '`puts` (**put** **s**tring) is a way of printing information to the user of your program.'
14-
message 'Take some time to contemplate the output of `puts` in irb:'
15-
result <<-RESULT
16-
1.9.3p125 :006 > puts 'Hello World'
17-
Hello World
18-
=> nil
19-
1.9.3p125 :007 >
20-
RESULT
21-
message 'The method `puts` always has the **return value** of `nil`, which is what we see after the `=>` in the output. Printing \'Hello World\' to the screen is just a side-effect.'
22-
end
23-
24-
step do
25-
irb <<-IRB
26-
fruits = ['peach', 'plum', 'pear']
27-
fruits.each { |fruit| puts fruit }
28-
IRB
29-
message 'The straight up-and-down `|` is called the \'pipe character\', and is typically the shifted version of the `\` (backslash) on your keyboard.'
30-
message 'Loops are a way of doing something here multiple times. In this loop, we printed each fruit to the screen in order.'
31-
end
32-
33-
step do
34-
irb <<-IRB
35-
numbers = [109, 10, 1001]
36-
numbers.each { |n| puts n * 2 }
37-
IRB
38-
message 'The curly braces here define a **block**, and whatever\'s in the pipes is a **block variable**.'
39-
message '`each` takes the first element in the array and sends it to the block, which temporarily stores it in the **block variable** and then runs the code after the pipes. It then goes back and does this again for each of the remaining items in the array.'
40-
irb <<-'IRB'
41-
ducks = ['huey', 'dewey', 'louie']
42-
ducks.each { |duck| puts "#{duck} quacks!" }
43-
ducks.each { |zombie| puts "#{zombie} quacks!" }
44-
IRB
45-
message 'It doesn\'t matter what you call your block variable: the previous two statements are exactly equivalent to Ruby. But you should try to name your variables something useful so the code makes sense to you later!'
46-
end
47-
48-
step do
49-
irb <<-'IRB'
50-
total = 256 ** 3
51-
colors = ['red', 'blue', 'green']
52-
colors.each do |color|
53-
puts "#{total} colors of paint on the wall..."
54-
puts "Take #{color} down, pass it around..."
55-
total = total - 1
56-
puts "#{total} colors of paint on the wall!"
57-
end
58-
IRB
59-
message "*The ** operator means 'to the power of', as in '256 to the third power'*"
60-
message "There's more than one way to make a block in ruby. The `do ... end` syntax is typically used when a block needs to span multiple lines, while the `{ ... }` syntax is for a single line block."
61-
end
62-
63-
explanation do
64-
message "As you build complex programs, you'll want to do something to many pieces of data without typing it all out. Loops help solve this problem."
65-
end
66-
67-
next_step 'running_programs_from_a_file'
1+
goals do
2+
goal "Use loops to do operations for every element in an array."
3+
goal do
4+
rawtext(md2html("Use `puts` to print strings to the screen."))
5+
end
6+
goal 'Learn the two different syntaxes for creating blocks in Ruby.'
7+
end
8+
9+
step do
10+
irb <<-IRB
11+
puts 'Hello World'
12+
IRB
13+
message '`puts` (**put** **s**tring) is a way of printing information to the user of your program.'
14+
message 'Take some time to contemplate the output of `puts` in irb:'
15+
result <<-RESULT
16+
1.9.3p125 :006 > puts 'Hello World'
17+
Hello World
18+
=> nil
19+
1.9.3p125 :007 >
20+
RESULT
21+
message 'The method `puts` always has the **return value** of `nil`, which is what we see after the `=>` in the output. Printing \'Hello World\' to the screen is just a side-effect.'
22+
end
23+
24+
step do
25+
irb <<-IRB
26+
fruits = ['peach', 'plum', 'pear']
27+
fruits.each { |fruit| puts fruit }
28+
IRB
29+
message 'The straight up-and-down `|` is called the \'pipe character\', and is typically the shifted version of the `\` (backslash) on your keyboard.'
30+
message 'Loops are a way of doing something here multiple times. In this loop, we printed each fruit to the screen in order.'
31+
end
32+
33+
step do
34+
irb <<-IRB
35+
numbers = [109, 10, 1001]
36+
numbers.each { |n| puts n * 2 }
37+
IRB
38+
message 'The curly braces here define a **block**, and whatever\'s in the pipes is a **block variable**.'
39+
message '`each` takes the first element in the array and sends it to the block, which temporarily stores it in the **block variable** and then runs the code after the pipes. It then goes back and does this again for each of the remaining items in the array.'
40+
irb <<-'IRB'
41+
ducks = ['huey', 'dewey', 'louie']
42+
ducks.each { |duck| puts "#{duck} quacks!" }
43+
ducks.each { |zombie| puts "#{zombie} quacks!" }
44+
IRB
45+
message 'It doesn\'t matter what you call your block variable: the previous two statements are exactly equivalent to Ruby. But you should try to name your variables something useful so the code makes sense to you later!'
46+
end
47+
48+
step do
49+
irb <<-'IRB'
50+
total = 256 ** 3
51+
colors = ['red', 'blue', 'green']
52+
colors.each do |color|
53+
puts "#{total} colors of paint on the wall..."
54+
puts "Take #{color} down, pass it around..."
55+
total = total - 1
56+
puts "#{total} colors of paint on the wall!"
57+
end
58+
IRB
59+
message "The ** operator means 'to the power of', as in '256 to the third power'"
60+
message "There's more than one way to make a block in ruby. The `do ... end` syntax is typically used when a block needs to span multiple lines, while the `{ ... }` syntax is for a single line block."
61+
end
62+
63+
explanation do
64+
message "As you build complex programs, you'll want to do something to many pieces of data without typing it all out. Loops help solve this problem."
65+
end
66+
67+
next_step 'running_programs_from_a_file'

0 commit comments

Comments
 (0)