[ next ] [ prev ] [ contents ] [ up to Ruby Immersion ] Using Ruby

Composite Pattern

class MasterInstaller
  def initialize
    @installers = []
  end

  def register(installer)
    @installers << installer
  end

  def install
    for installer in @installers
      installer.install
    end
  end

  def uninstall
    @installers.each { |installer|
      installer.uninstall
    }
  end

  def backup
    @installers.each do |installer|
      installer.backup
    end
  end
end

  • << is the polymorphic append operator.

  • { ... } is a code block. It is automatically passed to the each function.

  • |installer| is an argument to the code block.

  • list.each { |item| code } is equivalent to the for loop

  • do ... end is also code block.


[ next ] [ prev ] [ contents ] [ up to Ruby Immersion ] Copyright 2003 by Jim Weirich.
Some Rights Reserved