Ruby Overview

Much of this content is covered in chapter 4 of the book (Agile Web Development with Rails). Also see this online Ruby tutorial.

Objects and Classes

We will review these concepts with the Marble Jar classes. Example code.

Covered concepts:

These concepts are generally covered on p. 39 and pp. 45-46.

Core Classes (data structures)

See pp. 40 - 41

Basic control structures

Pages 42 - 44 cover 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