Help with UnboundMethod#bind error

G

gabriele renzi

Hi gurus and nubys,

I'm having a little problem with unbound methods.
I supposed I could unbound a method from Kernel
and put it in my own module, but it seems' I'm failing:

# this should go in Kernel
def met
p 'met'+self.to_s
end

m=Kernel.method( :met).unbind
module Mod;end

m.bind(Mod) #failing!
#but M.kind_of?(Kernel.class) #=>true


the error is :
Ruby1:13:in `bind': singleton method called for a different
object(TypeError)


now: I know that Mod is a different object. Anyway MEthod#bind should
worry just about obj.class, am I wrong ?

Or the problem is that :met is a singleton method ?
If so, why I can't detach a singleton method ?
 
N

nobu.nokada

Hi,

At Mon, 30 Jun 2003 00:31:00 +0900,
gabriele said:
I'm having a little problem with unbound methods.
I supposed I could unbound a method from Kernel
and put it in my own module, but it seems' I'm failing:

Although I'm not sure what you really want, try instance_method
instead.
m=Kernel.method( :met).unbind m=Kernel.instance_method:)met)

now: I know that Mod is a different object. Anyway MEthod#bind should
worry just about obj.class, am I wrong ?

Modules and classes always have metaclasses.

It might be ok to ignore metaclasses have no influence.


Index: eval.c
===================================================================
RCS file: /cvs/ruby/src/ruby/eval.c,v
retrieving revision 1.470
diff -u -2 -p -r1.470 eval.c
--- eval.c 28 Jun 2003 03:29:00 -0000 1.470
+++ eval.c 29 Jun 2003 16:34:32 -0000
@@ -7269,4 +7269,8 @@ mnew(klass, obj, id, mklass)
}

+ while (rklass != klass &&
+ (FL_TEST(rklass, FL_SINGLETON) || TYPE(rklass) == T_ICLASS)) {
+ rklass = RCLASS(rklass)->super;
+ }
if (TYPE(klass) == T_ICLASS) klass = RBASIC(klass)->klass;
method = Data_Make_Struct(mklass, struct METHOD, bm_mark, free, data);
 
G

gabriele renzi

il Mon, 30 Jun 2003 01:43:03 +0900, (e-mail address removed) ha
scritto::

Although I'm not sure what you really want, try instance_method
instead.

I was wondering of a quick way to decorate a method:
- unbind the method my_met() from its current object
(actually, I was thinking about method defined out of class
definitions, 'function'-like )
- put my_met() in a different module
- define a new my_met() using the old one.


m=Kernel.instance_method:)met)

well, I just thought the same thing,
I had the thing working in this way:
=> #<Method: Module(Object)#met>

If I try Kernel.instance_method I have a NameError.
Modules and classes always have metaclasses.

emh.. yeah I should have remembered this..
It might be ok to ignore metaclasses have no influence.

cool, that's the first time that I get a patch just for my own purpose
^_^

thank you very much


PS
anyway, my idea fails, it would have been to cool if it worked.

If I try to redefine my_met() as a "function" it
gets propagated to MyModule, and I fall in a 'stack level too deep'
error.

I suppose I need a freeze-like feature to avoid MyModule from
inherithing Kernel for a while.. and then inherithing it again..
Ok, I suppose I just need to sleep...
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Help with UnboundMethod#bind error"

|It might be ok to ignore metaclasses have no influence.

Check in your change, please.

matz.
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top