From 9d927204e7b86eb00bfd07a060a6383139edf741 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Tue, 30 Nov 2021 16:27:12 +0900 Subject: error.c: Let Exception#inspect inspect its message ... only when the message string has a newline. `p StandardError.new("foo\nbar")` now prints `#' instead of: # [Bug #18170] --- error.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'error.c') diff --git a/error.c b/error.c index 3f5c05d681..08b0c8da72 100644 --- a/error.c +++ b/error.c @@ -34,6 +34,7 @@ #include "internal/io.h" #include "internal/load.h" #include "internal/object.h" +#include "internal/string.h" #include "internal/symbol.h" #include "internal/thread.h" #include "internal/variable.h" @@ -1422,8 +1423,15 @@ exc_inspect(VALUE exc) str = rb_str_buf_new2("#<"); klass = rb_class_name(klass); rb_str_buf_append(str, klass); - rb_str_buf_cat(str, ": ", 2); - rb_str_buf_append(str, exc); + + if (RTEST(rb_str_include(exc, rb_str_new2("\n")))) { + rb_str_catf(str, ":%+"PRIsVALUE, exc); + } + else { + rb_str_buf_cat(str, ": ", 2); + rb_str_buf_append(str, exc); + } + rb_str_buf_cat(str, ">", 1); return str; -- cgit v1.2.3