Perry Smith wrote:
> ActiveRecord does this "trick".  For a normal object, this dump
> routine:
  <<snip>>
> Perry

FINALLY...

This simple program:

#!/usr/bin/env ruby

def dump(name, obj)
  puts "#{name}'s class: #{obj.class}"
  puts "#{name}'s ancestors: #{obj.class.ancestors.inspect}"
  puts "#{name}'s meta ancestors: #{(class << obj; self; 
end).ancestors.inspect}"
end

class Goofy
  alias_method :proxy_respond_to?, :respond_to?
  instance_methods.each { |m| undef_method m unless m =~ 
/(^__|^nil\?$|^send$|proxy_)/ }

  def initialize(o)
    @target = o
  end

  def respond_to?(symbol, include_priv = false)
    proxy_respond_to?(symbol, include_priv) || 
@target.respond_to?(symbol, include_priv)
  end

  private

  def method_missing(method, *args, &block)
    @target.send(method, *args, &block)
  end
end

g = Goofy.new("hi")

dump("g", g)

Produces this output:

g's class: String
g's ancestors: [String, Enumerable, Comparable, Object, Kernel]
g's meta ancestors: [Goofy, Object, Kernel]

Geeze that took me a long time to figure out.

-- 
Posted via http://www.ruby-forum.com/.