Skip to content

Commit 13d3f7a

Browse files
andrykonchineregon
authored andcommitted
Use Primitive.module_anonymous? in Struct#inspect
1 parent 5c636c0 commit 13d3f7a

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

spec/ruby/core/struct/fixtures/classes.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ def initialize(*args)
1313
end
1414
end
1515

16+
class StructWithOverriddenName < Struct.new(:a)
17+
def self.name
18+
"A"
19+
end
20+
end
21+
1622
class SubclassX < Struct
1723
end
1824

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,16 @@ class Foo < Struct.new(:a); end
2525

2626
m::Foo.new("").send(@method).should == '#<struct a="">'
2727
end
28+
29+
it "does not call #name method" do
30+
struct = StructClasses::StructWithOverriddenName.new("")
31+
struct.send(@method).should == '#<struct StructClasses::StructWithOverriddenName a="">'
32+
end
33+
34+
it "does not call #name method when struct is anonymous" do
35+
struct = Struct.new(:a)
36+
def struct.name; "A"; end
37+
38+
struct.new("").send(@method).should == '#<struct a="">'
39+
end
2840
end

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,11 @@ def to_s
145145
values << "#{var}=#{val.inspect}"
146146
end
147147

148-
name = self.class.name
149-
150-
if Primitive.nil?(name) || name.empty? || name[0] == '#'
148+
if Primitive.module_anonymous?(self.class)
151149
return "#<struct #{values.join(', ')}>"
152150
else
153-
return "#<struct #{self.class.name} #{values.join(', ')}>"
151+
name = Primitive.module_name self.class
152+
return "#<struct #{name} #{values.join(', ')}>"
154153
end
155154
end
156155

0 commit comments

Comments
 (0)