Much of this content is covered in Appendix A in the book.
We will review these concepts with the Marble Jar classes. Example code.
Covered concepts:
In addition to Appendix A, a "crash course" on class design to presented on pp. 56 - 57.
Example: list = ["cat", "dog", "cow"]
Example: table = {"dog" => "ruff", "cat" => "meow", "cow" => "moo"}
The appendix covers basic control structures such as if, unless,
while and for loops. The block structure is not found in many
languages and is worth a closer look. The block is a set of ruby
statements that is passed to a method. The block can be represented
with do or curly braces. The remaining examples use
blocks.
Here are three ways of looping through a list (array) of items:
for item in list
puts item
end
list.each do |item|
puts item
end
list.each {|item| puts item}
The times method repeats the given block of code the number of items specified by the objection:
count = 10
count.times do |round|
puts "We are on round number #{round}"
end