From: ara.t.howard@... Date: 2007-04-17T07:23:19+09:00 Subject: Re: "restoring" classes after changing stuff On Tue, 17 Apr 2007, Marcelo Alvim wrote: > Hi, > > Well, I'm new to Ruby (I've been working and playing with it for the > last 6 months only), and > I know there's probably another way in which this could work, but > somehow I feel like the > code below should work. Let's see what you guys think about it. > > The idea is making a copy of a class object, changing it and then > restoring it back to what it was after playing with the new stuff I > added. It should go somewhat like this: > you can't easily undo class changes. better to not make them in the first place. this will work harp:~ > cat a.rb module ProxyClass def self.new parent Class.new(parent) do const_set :PARENT, parent instance_methods.each{|m| undef_method m unless m[%r/__/]} c = self and define_method(:class){ c } def method_missing m, *a, &b self.class::PARENT.instance_method(m).bind(self).call(*a, &b) end end end end class Class def proxy() ProxyClass.new(self) end end A = Array.proxy class A def foo() :foo end end a = A[ 0, 1, 2 ] p a.class p a[0] p a.first(2) p a.foo harp:~ > ruby a.rb A 0 [0, 1] :foo regards. -a -- be kind whenever possible... it is always possible. - the dalai lama