Include Set subclass name in Set#inspect output

Fixes [Bug #21377]

Co-authored-by: zzak <zzak@hey.com>
This commit is contained in:
Jeremy Evans 2025-06-05 08:29:56 -07:00
parent 3a9c091cf3
commit 7c3bbfcddb
2 changed files with 19 additions and 5 deletions

20
set.c
View file

@ -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) return rb_usascii_str_new2("Set[...]");
str = rb_str_buf_new2("Set[");
set_iter(set, set_inspect_i, str);
if (recur) {
str = rb_sprintf("%"PRIsVALUE"[...]", klass_name);
return rb_str_export_to_enc(str, rb_usascii_encoding());
}
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;

View file

@ -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