How would I write a function to create additional methods in a
class which act as wrappers around already existing methods?
Example.
class Test
def foo
puts "foo"
end
end
Now I want to call a magic procedure, like
double_it(Test, "foo", "foo2")
which would have the effect of doing
class Test
def foo2
foo
foo
end
end
I can do
Test.class_eval {
def foo2
foo
foo
end
}
to add methods, but I don't know how to write the method at run-time.
The real reason I want this is that I have a large number of methods
in a class, and each method has several related methods, which are
alternate ways to invoke it. It would be nice if I could auto-define
all of those related methods, rather than having to type them all out
explicitly. I just don't know how to do that.
i.e.
class BigClass
def foo ... end
def bar ... end
def baz ... end
def wibble ... end
end
add_derivative_methods(BigClass, :foo)
add_derivative_methods(BigClass, :bar)
add_derivative_methods(BigClass, :baz)
add_derivative_methods(BigClass, :wibble)
where "add_derivative_methods" would define all the extra methods for
me.
Any ideas?
--Johann
--
Johann Hibschman johann / physics.berkeley.edu