diff options
author | Yusuke Endoh <[email protected]> | 2021-11-30 16:27:12 +0900 |
---|---|---|
committer | Yusuke Endoh <[email protected]> | 2022-06-07 11:07:09 +0900 |
commit | 9d927204e7b86eb00bfd07a060a6383139edf741 (patch) | |
tree | 104a9f92a462773adb5d5916068fa9b00152ed9d /error.c | |
parent | dbfb3b1917dbe89816ffeea6046d2743c32bf6b6 (diff) |
error.c: Let Exception#inspect inspect its message
... only when the message string has a newline.
`p StandardError.new("foo\nbar")` now prints `#<StandardError: "foo\nbar">'
instead of:
#<StandardError:
bar>
[Bug #18170]
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/4857
Diffstat (limited to 'error.c')
-rw-r--r-- | error.c | 12 |
1 files changed, 10 insertions, 2 deletions
@@ -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; |