Skip to content

Commit f69fede

Browse files
nirvdrumst0012
andcommitted
Modify Struct#{inspect,to_s} to match MRI when the struct is nested inside of an anonymous class or module.
Co-authored-by: Stan Lo <[email protected]>
1 parent 9b4e6b8 commit f69fede

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Compatibility:
7676
* Fix `Enumerable` methods `each_cons` and `each_slice` to return receiver (#2733, @horakivo)
7777
* `Module` methods `#private`, `#public`, `#protected`, `#module_function` now returns their arguments like in CRuby 3.1 (#2733, @horakivo)
7878
* `Kernel#exit!`, killing Fibers and internal errors do not run code in `ensure` clauses anymore, the same as CRuby (@eregon).
79+
* Modify `Struct#{inspect,to_s}` to match MRI when the struct is nested inside of an anonymous class or module (@st0012, @nirvdrum).
7980

8081
Performance:
8182

spec/ruby/core/struct/shared/inspect.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,22 @@
22
it "returns a string representation without the class name for anonymous structs" do
33
Struct.new(:a).new("").send(@method).should == '#<struct a="">'
44
end
5+
6+
it "returns a string representation without the class name for structs nested in anonymous classes" do
7+
obj = Object.new
8+
obj.singleton_class.class_eval <<~DOC
9+
class Foo < Struct.new(:a); end
10+
DOC
11+
12+
obj.singleton_class::Foo.new("").send(@method).should == '#<struct a="">'
13+
end
14+
15+
it "returns a string representation without the class name for structs nested in anonymous modules" do
16+
m = Module.new
17+
m.module_eval <<~DOC
18+
class Foo < Struct.new(:a); end
19+
DOC
20+
21+
m::Foo.new("").send(@method).should == '#<struct a="">'
22+
end
523
end

src/main/ruby/truffleruby/core/struct.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def to_s
147147

148148
name = self.class.name
149149

150-
if Primitive.nil?(name) || name.empty?
150+
if Primitive.nil?(name) || name.empty? || name[0] == '#'
151151
return "#<struct #{values.join(', ')}>"
152152
else
153153
return "#<struct #{self.class.name} #{values.join(', ')}>"

0 commit comments

Comments
 (0)