Skip to content

Commit 7c3bbfc

Browse files
jeremyevanszzak
andcommitted
Include Set subclass name in Set#inspect output
Fixes [Bug #21377] Co-authored-by: zzak <[email protected]>
1 parent 3a9c091 commit 7c3bbfc

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

set.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -536,10 +536,14 @@ set_i_initialize_copy(VALUE set, VALUE other)
536536
static int
537537
set_inspect_i(st_data_t key, st_data_t arg)
538538
{
539-
VALUE str = (VALUE)arg;
540-
if (RSTRING_LEN(str) > 4) {
539+
VALUE *args = (VALUE*)arg;
540+
VALUE str = args[0];
541+
if (args[1] == Qtrue) {
541542
rb_str_buf_cat_ascii(str, ", ");
542543
}
544+
else {
545+
args[1] = Qtrue;
546+
}
543547
rb_str_buf_append(str, rb_inspect((VALUE)key));
544548

545549
return ST_CONTINUE;
@@ -549,10 +553,16 @@ static VALUE
549553
set_inspect(VALUE set, VALUE dummy, int recur)
550554
{
551555
VALUE str;
556+
VALUE klass_name = rb_class_path(CLASS_OF(set));
557+
558+
if (recur) {
559+
str = rb_sprintf("%"PRIsVALUE"[...]", klass_name);
560+
return rb_str_export_to_enc(str, rb_usascii_encoding());
561+
}
552562

553-
if (recur) return rb_usascii_str_new2("Set[...]");
554-
str = rb_str_buf_new2("Set[");
555-
set_iter(set, set_inspect_i, str);
563+
str = rb_sprintf("%"PRIsVALUE"[", klass_name);
564+
VALUE args[2] = {str, Qfalse};
565+
set_iter(set, set_inspect_i, (st_data_t)args);
556566
rb_str_buf_cat2(str, "]");
557567

558568
return str;

test/ruby/test_set.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,10 @@ def test_inspect
846846

847847
set1.add(set2)
848848
assert_equal('Set[Set[0], 1, 2, Set[1, 2, Set[...]]]', set2.inspect)
849+
850+
c = Class.new(Set)
851+
c.set_temporary_name("_MySet")
852+
assert_equal('_MySet[1, 2]', c[1, 2].inspect)
849853
end
850854

851855
def test_to_s

0 commit comments

Comments
 (0)