Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added keyword_init? to classes generated by Struct.new, Ruby 3.1 support #2909

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions spec/ruby/core/struct/keyword_init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,24 @@
struct = Struct.new(:arg)
struct.keyword_init?.should be_nil
end

it "returns nil for a struct that does specify keyword_init to be nil" do
struct = Struct.new(:arg,keyword_init: nil)
struct.keyword_init?.should be_nil
end

it "returns true or false for truthy values in general, not just true" do
struct = Struct.new(:arg,keyword_init: 1)
struct.keyword_init?.should be_true

struct = Struct.new(:arg,keyword_init: "")
struct.keyword_init?.should be_true

struct = Struct.new(:arg,keyword_init: [])
struct.keyword_init?.should be_true

struct = Struct.new(:arg,keyword_init: {})
struct.keyword_init?.should be_true
end
end
end
3 changes: 0 additions & 3 deletions spec/tags/core/struct/keyword_init_tags.txt

This file was deleted.

12 changes: 10 additions & 2 deletions src/main/ruby/truffleruby/core/struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class << self
alias_method :subclass_new, :new
end

def self.new(klass_name, *attrs, keyword_init: false, &block)
def self.new(klass_name, *attrs, keyword_init: nil, &block)
if klass_name
if Primitive.object_kind_of?(klass_name, Symbol) # Truffle: added to avoid exception and match MRI
attrs.unshift klass_name
Expand Down Expand Up @@ -92,7 +92,15 @@ def self.inspect

const_set :STRUCT_ATTRS, attrs
const_set :KEYWORD_INIT, keyword_init
end

def self.keyword_init?
kw_init = self.const_get(:KEYWORD_INIT)
return nil if Primitive.nil?(kw_init)

Primitive.as_boolean(kw_init)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems simpler and shorted to me to calculate value in the keyword_init? method:

def self.keyword_init?
   return nil if Primitive.undefined?(KEYWORD_INIT) || Primitive.nil?(KEYWORD_INIT)
   KEYWORD_INIT != false
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍 .

minor: self::KEYWORD_INIT looks a bit shorter than self.const_get(:KEYWORD_INIT) but should work too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, self::KEYWORD_INIT is better, const_get is a few more indirections and less readable too.


end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should indented two spaces less


const_set klass_name, klass if klass_name

Expand Down