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:

  • Object
  • Class
  • Instance methods (belong to an object)
  • Instance variables (belong to an object)
  • Accessor methods (access object properties)
  • Return values
  • Class methods
  • Class variables
  • Inheritance, super class, subclass

Core Classes (data structures)

See chapter 4 Data Types section

  • Arrays
  • Example: list = ["cat", "dog", "cow"]

  • Hashes
  • Example: table = {"dog" => "ruff", "cat" => "meow", "cow" => "moo"}

Basic control structures

The chapter 4 Logic section 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