ruby-changes:4546
From: ko1@a...
Date: Tue, 15 Apr 2008 19:08:24 +0900 (JST)
Subject: [ruby-changes:4546] kazu - Ruby:r16039 (ruby_1_8): * eval.c (method_receiver, method_name, method_owner): New
kazu 2008-04-15 19:07:59 +0900 (Tue, 15 Apr 2008) New Revision: 16039 Modified files: branches/ruby_1_8/ChangeLog branches/ruby_1_8/NEWS branches/ruby_1_8/eval.c branches/ruby_1_8/test/ruby/test_method.rb Log: * eval.c (method_receiver, method_name, method_owner): New methods; backported from 1.9. bug#19007 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ChangeLog?r1=16039&r2=16038&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/test/ruby/test_method.rb?r1=16039&r2=16038&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/NEWS?r1=16039&r2=16038&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/eval.c?r1=16039&r2=16038&diff_format=u Index: ruby_1_8/NEWS =================================================================== --- ruby_1_8/NEWS (revision 16038) +++ ruby_1_8/NEWS (revision 16039) @@ -167,6 +167,12 @@ Return an enumerator if no block is given. + * Method#receiver + * Method#name + * Method#owner + + New methods. + * Numeric#step Return an enumerator if no block is given. Index: ruby_1_8/ChangeLog =================================================================== --- ruby_1_8/ChangeLog (revision 16038) +++ ruby_1_8/ChangeLog (revision 16039) @@ -1,3 +1,8 @@ +Tue Apr 15 19:03:28 2008 Kazuhiro NISHIYAMA <zn@m...> + + * eval.c (method_receiver, method_name, method_owner): New + methods; backported from 1.9. bug#19007 + Tue Apr 15 18:39:14 2008 Kazuhiro NISHIYAMA <zn@m...> * lib/uri.rb, lib/uri/ldaps.rb: added LDAPS Index: ruby_1_8/test/ruby/test_method.rb =================================================================== --- ruby_1_8/test/ruby/test_method.rb (revision 16038) +++ ruby_1_8/test/ruby/test_method.rb (revision 16039) @@ -39,4 +39,13 @@ um.bind(Base.new) end end + + def test_receiver_name_owner + o = Object.new + def o.foo; end + m = o.method(:foo) + assert_equal(o, m.receiver) + assert_equal("foo", m.name) + assert_equal(class << o; self; end, m.owner) + end end Index: ruby_1_8/eval.c =================================================================== --- ruby_1_8/eval.c (revision 16038) +++ ruby_1_8/eval.c (revision 16039) @@ -9120,6 +9120,57 @@ /* * call-seq: + * meth.receiver => object + * + * Returns the bound receiver of the method object. + */ + +static VALUE +method_receiver(obj) + VALUE obj; +{ + struct METHOD *data; + + Data_Get_Struct(obj, struct METHOD, data); + return data->recv; +} + +/* + * call-seq: + * meth.name => string + * + * Returns the name of the method. + */ + +static VALUE +method_name(obj) + VALUE obj; +{ + struct METHOD *data; + + Data_Get_Struct(obj, struct METHOD, data); + return rb_str_new2(rb_id2name(data->oid)); +} + +/* + * call-seq: + * meth.owner => class_or_module + * + * Returns the class or module that defines the method. + */ + +static VALUE +method_owner(obj) + VALUE obj; +{ + struct METHOD *data; + + Data_Get_Struct(obj, struct METHOD, data); + return data->klass; +} + +/* + * call-seq: * obj.method(sym) => method * * Looks up the named method as a receiver in <i>obj</i>, returning a @@ -9739,6 +9790,9 @@ rb_define_method(rb_cMethod, "inspect", method_inspect, 0); 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, "receiver", method_receiver, 0); + rb_define_method(rb_cMethod, "name", method_name, 0); + rb_define_method(rb_cMethod, "owner", method_owner, 0); rb_define_method(rb_cMethod, "unbind", method_unbind, 0); rb_define_method(rb_mKernel, "method", rb_obj_method, 1); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/