"restoring" classes after changing stuff

M

Marcelo Alvim

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 Alvim.
 
A

ara.t.howard

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top