From 1fd181b453a6a2dd16897473afa2f402c7dba8aa Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Tue, 24 Jan 2023 17:40:57 +0900 Subject: error.c: Update the message format for NoMethodError * If the receiver is a Class, use "... for class ". * If the receiver is a Module, use "... for module ". * If the receiver is an extended object (i.e., has a singleton class), use "... for ". * Otherwise, use "... for an instance of ". Examples: ``` 42.time #=> undefined method `time' for an instance of Integer (NoMethodError) class Foo privatee #=> undefined local variable or method 'privatee' for class Foo (NoMethodError) end def (o=Object.new).foo end o.bar #=> undefined method `bar' for # (NoMethodError) ``` --- error.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'error.c') diff --git a/error.c b/error.c index affee7d828..727697542f 100644 --- a/error.c +++ b/error.c @@ -2116,12 +2116,14 @@ name_err_mesg_to_str(VALUE obj) object: klass = CLASS_OF(obj); if (RB_TYPE_P(klass, T_CLASS) && FL_TEST(klass, FL_SINGLETON)) { - s = FAKE_CSTR(&s_str, "extended object "); + s = FAKE_CSTR(&s_str, ""); + c = rb_any_to_s(obj); + break; } else { - s = FAKE_CSTR(&s_str, "object "); + s = FAKE_CSTR(&s_str, "an instance of "); + c = rb_class_real(klass); } - c = rb_class_real(klass); } c2 = rb_protect(name_err_mesg_receiver_name, c, &state); if (state || NIL_OR_UNDEF_P(c2)) -- cgit v1.2.1