diff options
author | Jeremy Evans <[email protected]> | 2025-06-05 08:29:56 -0700 |
---|---|---|
committer | Jeremy Evans <[email protected]> | 2025-06-25 09:21:07 +0900 |
commit | 7c3bbfcddb05b0eb7cca7ac32efd2fc07e1af6ec (patch) | |
tree | da391cb4332a347d30b7c48d3ddb9ca7af141409 | |
parent | 3a9c091cf393e8a9c4e4b93d4216f2be3678e488 (diff) |
Include Set subclass name in Set#inspect output
Fixes [Bug #21377]
Co-authored-by: zzak <[email protected]>
-rw-r--r-- | set.c | 20 | ||||
-rw-r--r-- | test/ruby/test_set.rb | 4 |
2 files changed, 19 insertions, 5 deletions
@@ -536,10 +536,14 @@ set_i_initialize_copy(VALUE set, VALUE other) static int set_inspect_i(st_data_t key, st_data_t arg) { - VALUE str = (VALUE)arg; - if (RSTRING_LEN(str) > 4) { + VALUE *args = (VALUE*)arg; + VALUE str = args[0]; + if (args[1] == Qtrue) { rb_str_buf_cat_ascii(str, ", "); } + else { + args[1] = Qtrue; + } rb_str_buf_append(str, rb_inspect((VALUE)key)); return ST_CONTINUE; @@ -549,10 +553,16 @@ static VALUE set_inspect(VALUE set, VALUE dummy, int recur) { VALUE str; + VALUE klass_name = rb_class_path(CLASS_OF(set)); + + if (recur) { + str = rb_sprintf("%"PRIsVALUE"[...]", klass_name); + return rb_str_export_to_enc(str, rb_usascii_encoding()); + } - if (recur) return rb_usascii_str_new2("Set[...]"); - str = rb_str_buf_new2("Set["); - set_iter(set, set_inspect_i, str); + str = rb_sprintf("%"PRIsVALUE"[", klass_name); + VALUE args[2] = {str, Qfalse}; + set_iter(set, set_inspect_i, (st_data_t)args); rb_str_buf_cat2(str, "]"); return str; diff --git a/test/ruby/test_set.rb b/test/ruby/test_set.rb index 110c509463..87e1fd8d26 100644 --- a/test/ruby/test_set.rb +++ b/test/ruby/test_set.rb @@ -846,6 +846,10 @@ class TC_Set < Test::Unit::TestCase set1.add(set2) assert_equal('Set[Set[0], 1, 2, Set[1, 2, Set[...]]]', set2.inspect) + + c = Class.new(Set) + c.set_temporary_name("_MySet") + assert_equal('_MySet[1, 2]', c[1, 2].inspect) end def test_to_s |