At Wed, 22 Nov 2000 04:02:20 +0900,
GOTO Kentaro wrote:

>   class Class
>     def whereis(meth)
[...]
>     end
>   end
> 
>   p Fixnum.whereis :nonzero?  #=> Numeric
>   p Fixnum.whereis :integer?  #=> Integer
>   p Fixnum.whereis :zero?     #=> Fixnum
>   p Array.whereis :find       #=> Enumerable

p Array.whereis :dup            #=> Kernel

is this ok?  I mean, should the module 'Kernel' exposed to end user?

anyway, I tried to implement in C level.  The patch still have one
problem.  When method is defined at the module 'Kernel', it prints as
follows:

$ ./ruby -e "p Integer.new.method(:dup).defined_at"
#<Kernel:0x401b7c94>
$ ./ruby -e "p Integer.new.method(:dup).defined_at.type"
Kernel

I'd very much appreciate if someone enlighten me.



for others, it seems working ok:

$ ./ruby -e "o Module.defined_at(:class_eval)"
Module
$ ./ruby -e "p Integer.new.method(:+@).defined_at"
Numeric

oh well..
--
         yashi

Index: eval.c =================================================================== RCS file: /opt/cvsroot/ruby/eval.c,v retrieving revision 1.124 diff -u -r1.124 eval.c --- eval.c 2000/11/08 05:29:21 1.124 +++ eval.c 2000/11/21 20:49:42 @@ -6461,6 +6461,36 @@ } static VALUE +method_defined_at(method) + VALUE method; +{ + struct METHOD *data; + + Data_Get_Struct(method, struct METHOD, data); + return data->klass; +} + +static VALUE +rb_obj_defined_at(self, arg) + VALUE self, arg; +{ + ID id; + NODE *body; + VALUE klass, orig; + + id = rb_to_id(arg); + klass = rb_class_of(self); + orig = klass; + + while (!st_lookup(RCLASS(klass)->m_tbl, id, &body)) { + klass = RCLASS(klass)->super; + if (!klass) + print_undef(orig, id); + } + return klass; +} + +static VALUE mproc() { VALUE proc; @@ -6538,7 +6568,9 @@ rb_define_method(rb_cMethod, "to_s", method_inspect, 0); rb_define_method(rb_cMethod, "to_proc", method_proc, 0); rb_define_method(rb_cMethod, "unbind", method_unbind, 0); + rb_define_method(rb_cMethod, "defined_at", method_defined_at, 0); rb_define_method(rb_mKernel, "method", rb_obj_method, 1); + rb_define_method(rb_mKernel, "defined_at", rb_obj_defined_at, 1); rb_cUnboundMethod = rb_define_class("UnboundMethod", rb_cMethod); rb_define_method(rb_cUnboundMethod, "call", umethod_call, -1);