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

Object Orientation

class FileInstaller
  def initialize(file, target_dir)
    @file = file
    @target_dir = target_dir
  end

  def install
    puts "Installing file #{@target_dir}/#{@file}"
    # Code to install a file
  end
    
  def uninstall
    puts "Uninstalling file #{@target_dir}/#{@file}"
    # Code to UN-install a file
  end

  def backup
    puts "Backing up file #{@target_dir}/#{@file}"
    # Code to create a backup
  end
end

  • Class introduced by the class keyword

  • Methods are defined by def

  • initialize is like a constructor

  • @file and @target_dir are instance variables. All instance variables begin with "@".


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