Skip to content

Commit 0435c76

Browse files
committed
Refactor Struct.new method and simplify handling a class name parameter
1 parent 76375a7 commit 0435c76

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,14 @@ class << self
3333
alias_method :subclass_new, :new
3434
end
3535

36-
def self.new(klass_name = nil, *attrs, keyword_init: nil, &block)
37-
if klass_name
38-
if Primitive.is_a?(klass_name, Symbol) # Truffle: added to avoid exception and match MRI
39-
attrs.unshift klass_name
40-
klass_name = nil
41-
else
42-
begin
43-
klass_name = StringValue klass_name
44-
rescue TypeError
45-
attrs.unshift klass_name
46-
klass_name = nil
47-
end
48-
end
36+
def self.new(*attrs, keyword_init: nil, &block)
37+
klass_name = nil
38+
39+
first = attrs[0]
40+
if attrs.size >= 1 && !Primitive.is_a?(first, Symbol)
41+
# nil check because Struct.new(nil, :foo) is valid
42+
klass_name = StringValue(first) unless Primitive.nil?(first)
43+
attrs.shift
4944
end
5045

5146
attrs = attrs.map { |a| Truffle::Type.symbol_or_string_to_symbol(a) }

0 commit comments

Comments
 (0)