A more complex, but still simple example:

class Microwave # defrosts food
  attr_accessor :creators
  def initialize()
    @creators = []
  end
  def mode(key = nil, cls = nil)
    delegate = cls ? (Proc.new {|path| cls.new(path)}) : Proc.new
    creators << [key, delegate]
  end

  def defrost(path)
    r = nil
    creators.each do |(key, delegate)|
      next unless (case key
        when nil then true
        when Regexp then key =~ path
        when String then key == path
        else false
      end)
      return r if (r = delegate.call(path))
    end
  end
end

# A delectable bit wrap. Whole wheat pita!
class FileWrap
  attr_accessor :contents
  def initialize(path)
    @path = path
    @contents = IO.read(path)
  end
  def method_missing(sel, *args)
    r = @contents.send(sel, *args)
    if sel.to_s[-1..-1] == '!'
      (File.open(@path, 'w') {|f| f.write(@contents)})
    end
    r
  end
end

M = Microwave.new

M.mode(/\.txt$/, FileWrap)
M.mode(/\.ruby_object$/) {|path| Marshal.load(IO.read(path))}

# Put some stuff in the fridge
File.open('/tmp/a_hash.ruby_object', 'w') do |f|
  f.write(Marshal.dump(:a => 2, 'a' => 'two'))
end
File.open('/tmp/a_file.txt', 'w') do |f|
  f.write('Hello World')
end

h = M.defrost('/tmp/a_hash.ruby_object')
puts h['a'] #=> two
puts h[:a] #=> 2

f = M.defrost('/tmp/a_file.txt')
puts f.contents #=> Hello World
f.gsub!('World', 'Food')
puts `cat /tmp/a_file.txt` #=> Hello Food

Well, it was simple. Then I became carried away.

-- 

Nicholas Seckar aka. Ulysses