Skip to content

Commit 51b497f

Browse files
committed
[GR-19220] Implement no args Struct.new constructor in Ruby 3.3 (#3491)
PullRequest: truffleruby/4246
2 parents aa8b3f1 + 9f9e1d6 commit 51b497f

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

spec/tags/core/struct/new_tags.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fails:Struct.new raises ArgumentError if not provided any arguments

spec/truffleruby.next-specs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# Use spec/ruby/core/nil/nil_spec.rb as a dummy file to avoid being empty (what causes mspec to error)
1010
spec/ruby/core/nil/nil_spec.rb
1111

12+
spec/ruby/core/struct/new_spec.rb
1213
spec/ruby/core/warning/element_reference_spec.rb
1314
spec/ruby/core/warning/element_set_spec.rb
1415

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, *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)