From 99213dc2df2d3b76e0139956cb6257378961c821 Mon Sep 17 00:00:00 2001 From: Vasiliy Ermolovich Date: Mon, 2 Apr 2018 18:22:19 +0300 Subject: [PATCH] Fix Kernel#singleton_method with Module#Prepend. --- proc.c | 6 +++--- test/ruby/test_method.rb | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/proc.c b/proc.c index 7134f68..551a135 100644 --- a/proc.c +++ b/proc.c @@ -1771,11 +1771,11 @@ VALUE rb_obj_singleton_method(VALUE obj, VALUE vid) { const rb_method_entry_t *me; - VALUE klass; + VALUE klass = RCLASS_ORIGIN(rb_singleton_class_get(obj)); ID id = rb_check_id(&vid); if (!id) { - if (!NIL_P(klass = rb_singleton_class_get(obj)) && + if (!NIL_P(klass) && respond_to_missing_p(klass, obj, vid, FALSE)) { id = rb_intern_str(vid); return mnew_missing(klass, obj, id, rb_cMethod); @@ -1784,7 +1784,7 @@ rb_obj_singleton_method(VALUE obj, VALUE vid) rb_name_err_raise("undefined singleton method `%1$s' for `%2$s'", obj, vid); } - if (NIL_P(klass = rb_singleton_class_get(obj)) || + if (NIL_P(klass) || UNDEFINED_METHOD_ENTRY_P(me = rb_method_entry_at(klass, id)) || UNDEFINED_REFINED_METHOD_P(me->def)) { vid = ID2SYM(id); diff --git a/test/ruby/test_method.rb b/test/ruby/test_method.rb index 0ef913f..67c24a8 100644 --- a/test/ruby/test_method.rb +++ b/test/ruby/test_method.rb @@ -782,6 +782,16 @@ def o.bar; :bar; end assert_equal(:bar, m.call, feature8391) end + def test_singleton_method_prepend + bug14658 = '[Bug #14658]' + c1 = Class.new + o = c1.new + def o.bar; :bar; end + class << o; prepend Module.new; end + m = assert_nothing_raised(NameError, bug14658) {o.singleton_method(:bar)} + assert_equal(:bar, m.call, bug14658) + end + Feature9783 = '[ruby-core:62212] [Feature #9783]' def assert_curry_three_args(m) -- 2.1.1