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

Message Based Polymorphism

class Proxy
  def initialize(target)
    @target = target
  end
  def method_missing(sym, *args, &block)
    @target.send(sym, *args, &block)
  end
end

class Dog
  def talk; puts "WOOF"; end
end

d = Dog.new
p = Proxy.new(d)

p.talk

Output

WOOF

  • Proxy classes become trival to write
  • Other applications: Mock Objects, Remote objects


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