This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Extending Specific Objects with Modules (written by Phil Tomson)
CodeExpectedActual
class Person
  attr_reader :name, :age, :occupation
  def initialize(name, age, occupation)
    @name, @age, @occupation = name, age, occupation
  end    
  def mild_mannered?
    true
  end
end
jimmy = Person.new('Jimmy Olsen', 21, 'cub reporter')
clark = Person.new('Clark Kent', 35, 'reporter')
jimmy.mild_mannered?
true true
clark.mild_mannered?
true true
module SuperPowers
  def fly
    'Flying!'
  end
  def leap(what)    
    "Leaping #{what} in a single bound!"
  end
  def mild_mannered?
    false
  end
  def superhero_name
    'Superman'
  end
end
clark.extend(SuperPowers)
clark.superhero_name
"Superman" "Superman"
clark.fly
"Flying!" "Flying!"
clark.mild_mannered?
false false
jimmy.mild_mannered?
true true
class Person  
  extend SuperPowers
end
#which is equivalent to:
Person.extend(SuperPowers)
Person.superhero_name
"Superman" "Superman"
Person.fly
"Flying!" "Flying!"