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

Remove OpenStruct usage, will warn in Ruby 3.4, raise in Ruby 3.5 #321

Open
Earlopain opened this issue Apr 2, 2024 · 2 comments · May be fixed by #322
Open

Remove OpenStruct usage, will warn in Ruby 3.4, raise in Ruby 3.5 #321

Earlopain opened this issue Apr 2, 2024 · 2 comments · May be fixed by #322
Assignees

Comments

@Earlopain
Copy link

Earlopain commented Apr 2, 2024

Ruby 3.4 will issue a warning for this, and Ruby 3.5 will raise. Also see https://bugs.ruby-lang.org/issues/20309, ruby/ruby#10428

There is a single place here that will need adjusting:

def self.from_hash(h)
# Create a "fake" response object with the data passed from h
self.new OpenStruct.new(h)
end

The other places are test only and can be fixed simply by adding it to the Gemfile (or alternativly just return a hash from response_generator and use the from_hash method, seems like that should work). I suggest replacing the one usage with the following (untested, should be fine):

ResponseHash = Struct.new(:body, :code)

def self.from_hash(h)
  # Create a "fake" response object with the data passed from h
  self.new ResponseHash.new(h[:body] , h[:code])
end
@Earlopain Earlopain changed the title Remove OpenStruct usage, will warn in Ruby 3.5, raise in Ruby 3.6 Remove OpenStruct usage, will warn in Ruby 3.4, raise in Ruby 3.5 Apr 2, 2024
@mgrishko mgrishko self-assigned this Apr 3, 2024
@frederikspang
Copy link

Furthermore, OpenStruct usage is generally discouraged, due to invalidating the global method cache in Ruby.

From RuboCop documentation:

Instantiation of an OpenStruct invalidates Ruby global method cache as it causes dynamic method definition during program runtime.

@Earlopain
Copy link
Author

Earlopain commented Sep 10, 2024

Ruby 3.3.5 now warns about this.

irb(main):001> require "mailgun"
/home/user/Documents/mailgun-ruby/lib/mailgun.rb:8: warning: ostruct was loaded from the standard library, but will no longer be part of the default gems starting from Ruby 3.5.0.
You can add ostruct to your Gemfile or gemspec to silence this warning.

#322

Earlopain added a commit to Earlopain/mailgun-ruby that referenced this issue Sep 10, 2024
@Earlopain Earlopain linked a pull request Sep 10, 2024 that will close this issue
Earlopain added a commit to Earlopain/mailgun-ruby that referenced this issue Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants