goals do
goal "Use loops to do operations for every element in an array."
goal do
rawtext(md2html("Use `puts` to print strings to the screen."))
end
goal 'Learn the two different syntaxes for creating blocks in Ruby.'
end
step do
irb <<-IRB
puts 'Hello World'
IRB
message '`puts` (**put** **s**tring) is a way of printing information to the user of your program.'
message 'Take some time to contemplate the output of `puts` in irb:'
result <<-RESULT
1.9.3p125 :006 > puts 'Hello World'
Hello World
=> nil
1.9.3p125 :007 >
RESULT
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.'
end
step do
irb <<-IRB
fruits = ['peach', 'plum', 'pear']
fruits.each { |fruit| puts fruit }
IRB
message 'The straight up-and-down `|` is called the \'pipe character\', and is typically the shifted version of the `\` (backslash) on your keyboard.'
message 'Loops are a way of doing something here multiple times. In this loop, we printed each fruit to the screen in order.'
end
step do
irb <<-IRB
numbers = [109, 10, 1001]
numbers.each { |n| puts n * 2 }
IRB
message 'The curly braces here define a **block**, and whatever\'s in the pipes is a **block variable**.'
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.'
irb <<-'IRB'
ducks = ['huey', 'dewey', 'louie']
ducks.each { |duck| puts "#{duck} quacks!" }
ducks.each { |zombie| puts "#{zombie} quacks!" }
IRB
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!'
end
step do
irb <<-'IRB'
total = 256 ** 3
colors = ['red', 'blue', 'green']
colors.each do |color|
puts "#{total} colors of paint on the wall..."
puts "Take #{color} down, pass it around..."
total = total - 1
puts "#{total} colors of paint on the wall!"
end
IRB
message "The ** operator means 'to the power of', as in '256 to the third power'"
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."
end
explanation do
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."
end
next_step 'running_programs_from_a_file'