Skip to content

Commit

Permalink
[GR-19220] Implement no args Struct.new constructor in Ruby 3.3 (#3491)
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/4246
  • Loading branch information
andrykonchin committed Apr 19, 2024
2 parents aa8b3f1 + 9f9e1d6 commit 51b497f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
1 change: 1 addition & 0 deletions spec/tags/core/struct/new_tags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fails:Struct.new raises ArgumentError if not provided any arguments
1 change: 1 addition & 0 deletions spec/truffleruby.next-specs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# Use spec/ruby/core/nil/nil_spec.rb as a dummy file to avoid being empty (what causes mspec to error)
spec/ruby/core/nil/nil_spec.rb

spec/ruby/core/struct/new_spec.rb
spec/ruby/core/warning/element_reference_spec.rb
spec/ruby/core/warning/element_set_spec.rb

Expand Down
21 changes: 8 additions & 13 deletions src/main/ruby/truffleruby/core/struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,14 @@ class << self
alias_method :subclass_new, :new
end

def self.new(klass_name, *attrs, keyword_init: nil, &block)
if klass_name
if Primitive.is_a?(klass_name, Symbol) # Truffle: added to avoid exception and match MRI
attrs.unshift klass_name
klass_name = nil
else
begin
klass_name = StringValue klass_name
rescue TypeError
attrs.unshift klass_name
klass_name = nil
end
end
def self.new(*attrs, keyword_init: nil, &block)
klass_name = nil

first = attrs[0]
if attrs.size >= 1 && !Primitive.is_a?(first, Symbol)
# nil check because Struct.new(nil, :foo) is valid
klass_name = StringValue(first) unless Primitive.nil?(first)
attrs.shift
end

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

0 comments on commit 51b497f

Please sign in to comment.