diff --git a/ChangeLog b/ChangeLog index dd7c132631..2e394ce9c5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Tue Apr 15 19:03:28 2008 Kazuhiro NISHIYAMA + + * 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 * lib/uri.rb, lib/uri/ldaps.rb: added LDAPS diff --git a/NEWS b/NEWS index 6ebec2d2c5..ea98fc2d8e 100644 --- a/NEWS +++ b/NEWS @@ -167,6 +167,12 @@ with all sufficient information, see the ChangeLog file. 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. diff --git a/eval.c b/eval.c index 5a03eccc91..fbca6a0fec 100644 --- a/eval.c +++ b/eval.c @@ -9118,6 +9118,57 @@ method_unbind(obj) return method; } +/* + * 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 @@ -9739,6 +9790,9 @@ Init_Proc() 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); diff --git a/test/ruby/test_method.rb b/test/ruby/test_method.rb index c30705cb15..b22c2cba62 100644 --- a/test/ruby/test_method.rb +++ b/test/ruby/test_method.rb @@ -39,4 +39,13 @@ class TestMethod < Test::Unit::TestCase 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