aboutsummaryrefslogtreecommitdiffstats
path: root/error.c
diff options
context:
space:
mode:
authorYusuke Endoh <mame@ruby-lang.org>2023-01-24 17:40:57 +0900
committerYusuke Endoh <mame@ruby-lang.org>2023-02-20 10:33:06 +0900
commit1fd181b453a6a2dd16897473afa2f402c7dba8aa (patch)
tree434aa52c074a3729e85a225b4210da163d39549a /error.c
parente7b8d32e166815f2e7edebf32aa178915d191b8c (diff)
downloadruby-1fd181b453a6a2dd16897473afa2f402c7dba8aa.tar.gz
error.c: Update the message format for NoMethodError
* If the receiver is a Class, use "... for class <class name>". * If the receiver is a Module, use "... for module <module name>". * If the receiver is an extended object (i.e., has a singleton class), use "... for <rb_any_to_s(receiver)>". * Otherwise, use "... for an instance of <class name>". 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 #<Object: 0xdeadbeef(any_to_s)> (NoMethodError) ```
Diffstat (limited to 'error.c')
-rw-r--r--error.c8
1 files changed, 5 insertions, 3 deletions
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))