Ruby Overview

Much of this content is covered in Appendix A in the book.

Objects and Classes

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.

Core Classes (data structures)

Basic control structures

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