[Ruby] copying methods around
Aaron Patterson
aaron.patterson at gmail.com
Tue Mar 20 16:46:35 PST 2007
On 3/20/07, John Whitley <whitley at bangpath.org> wrote:
>
> Aaron Patterson wrote:
> > I'm sure there's an easy way to do this, but I can't seem to find
> > it! I
> > want to copy a method from one class to another instance.
>
> Does the source method have to be in a class? Can you stuff it into
> a module on its own then just OpenStruct.include(SussudioModule) (or
> reopen OpenStruct, etc.).
Not really.... I thought of that, but the problem is that I'm defining many
methods at run time, and I need to pick and choose which methods get set on
the OpenStruct instance.
I note that the presented implementation seems odd out of context, in
> that you're both returning a new instance of OpenStruct as well as
> side-affecting the OpenStruct class.
Yes, it is odd. I am trying to convert Javascript to Ruby, and I'm
currently working on supporting javascript objects. In javascript (to
create an object), you just define a bunch of functions, then bind the
function to an object. That is basically what I am trying to accomplish.
The only problem is that the function can be called on its own without being
bound to any particular object!
So far, the solutions that I've found are:
1) ruby2ruby trickery, 2) define all methods on all objects (like your
suggestion), or 3) delegate the method call to another object, and that
other object keeps some sort of context.
--
Aaron Patterson
http://tenderlovemaking.com/
More information about the Ruby
mailing list