# "restoring" classes after changing stuff

**URL:** <https://www.ruby-forum.com/t/restoring-classes-after-changing-stuff/92189>\
**Category:** Ruby\
**Created:** [April 16, 2007, 11:35pm UTC](https://www.ruby-forum.com/t/restoring-classes-after-changing-stuff/92189 "2007-04-16T23:35:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcelo\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@marcelo\_a](https://www.ruby-forum.com/u/marcelo_a)\
**Post date:** [April 16, 2007, 11:35pm UTC](https://www.ruby-forum.com/t/restoring-classes-after-changing-stuff/92189/1 "2007-04-16T23:35:49Z")

</div>

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:

OldArray = Array.dup

# =\> OldArray

class Array  
def my\_method; “My Method”; end  
end

# =\> nil

[].my\_method

# =\> “My Method”

x = Array.new

# =\> []

x.my\_method

# =\> “My Method”

### Switch back

Array = OldArray.dup  
(irb):9: warning: already initialized constant Array

# =\> Array

x.my\_method

# =\> “My Method”

(Ok, now why did that work? Maybe “x” is still using the modified Array  
class?)

[].my\_method

# =\> “My Method”

(Same thing here, it seems)

[1, 2, 3].my\_method

# =\> “My Method”

(Hmmmm, let’s try something different)

x = Array.new

# =\> []

x.my\_method  
NoMethodError: undefined method `my\_method’ for []:Array  
from (irb):14  
from :0

(Ok, so new instances only? Is this weird at all, or is this the way  
it’s supposed to work?)

I’m asking these questions out of real lack of knowledge, and I REALLY  
don’t suppose  
something should change in order to make this work. I’m just trying to  
understand how  
Ruby does all this stuff.

Thank you in advance,  
Marcelo A…

---

<div class="post-metadata">

**Author:** ![marcelo\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@marcelo\_a](https://www.ruby-forum.com/u/marcelo_a)\
**Post date:** [April 17, 2007, 12:23am UTC](https://www.ruby-forum.com/t/restoring-classes-after-changing-stuff/92189/2 "2007-04-17T00:23:54Z")

</div>

On Tue, 17 Apr 2007, Marcelo A. wrote:

> 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
