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

Fix Delegator to pass keyword arguments for Ruby 3.0 #1684

Merged
merged 4 commits into from
Apr 17, 2021

Conversation

andrewtblake
Copy link

When Ruby is upgraded to 3.0, errors result from keyword arguments not being passed correctly through the Delegator delegate method. This PR fixes the issue.

@dentarg
Copy link
Member

dentarg commented Mar 20, 2021

Looks like this change isn't compatible with Ruby < 2.7

Would also be great to have a test for this (which fails on Ruby 3.0 until the problem has been fixed)

@andrewtblake
Copy link
Author

andrewtblake commented Mar 20, 2021 via email

@andrewtblake
Copy link
Author

andrewtblake commented Mar 20, 2021 via email

- passes on Ruby < 2.7
- passes with warning on Ruby >= 2.7, < 3.0
- fails on Ruby 3.0
@dentarg
Copy link
Member

dentarg commented Mar 22, 2021

@andrewtblake You can add your test from #1686 to this branch

From reading https://eregon.me/blog/2021/02/13/correct-delegation-in-ruby-2-27-3.html it sounds like we need to use ruby2_keywords. Maybe you can experiment with it?

At https://github.com/ruby/ruby2_keywords#usage there's an example that is using define_method

@andrewtblake
Copy link
Author

andrewtblake commented Mar 22, 2021 via email

@andrewtblake
Copy link
Author

andrewtblake commented Mar 22, 2021 via email

Copy link
Member

@dentarg dentarg left a comment

Choose a reason for hiding this comment

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

Nice work, I answered your questions about Gemfile/require


# We need ruby2_keywords to make delegation work correctly on
# ruby2.7 and ruby3
require 'ruby2_keywords'
Copy link
Member

Choose a reason for hiding this comment

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

I would put the require at the top of the file, somewhere at

# external dependencies
require 'rack'
require 'tilt'
require 'rack/protection'
require 'mustermann'
require 'mustermann/sinatra'
require 'mustermann/regular'

Gemfile Outdated
@@ -66,6 +66,7 @@ if RUBY_ENGINE == "ruby"
gem 'commonmarker', '~> 0.20.0'
gem 'pandoc-ruby', '~> 2.0.2'
gem 'simplecov', require: false
gem 'ruby2_keywords', '~> 0.0.4'
Copy link
Member

Choose a reason for hiding this comment

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

The Gemfile only holds our test dependencies, this will be a runtime dependency for Sinatra, so it needs to be in the gemspec:

sinatra/sinatra.gemspec

Lines 46 to 49 in cf40452

s.add_dependency 'rack', '~> 2.2'
s.add_dependency 'tilt', '~> 2.0'
s.add_dependency 'rack-protection', version
s.add_dependency 'mustermann', '~> 1.0'

@@ -1947,6 +1952,7 @@ def self.delegate(*methods)
return super(*args, &block) if respond_to? method_name
Delegator.target.send(method_name, *args, &block)
end
ruby2_keywords method_name
Copy link
Member

Choose a reason for hiding this comment

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

We can also avoid needing to use the ruby2_keywords gem if we do

Suggested change
ruby2_keywords method_name
ruby2_keywords(method_name) if respond_to?(:ruby2_keywords, true)

@andrewtblake
Copy link
Author

andrewtblake commented Mar 23, 2021 via email

@@ -1924,6 +1924,7 @@ class #{self.class}
# inherit all settings, routes, handlers, and error pages from the
# top-level. Subclassing Sinatra::Base is highly recommended for
# modular applications.

Copy link
Member

Choose a reason for hiding this comment

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

Let's drop this newline

@dentarg
Copy link
Member

dentarg commented Mar 25, 2021

@andrewtblake if you want you can squash all commits here so it's just one so we get a nicer history

FYI I'm not a maintainer so I can't merge this

Relocate ruby2_keywords gem dependency and `require` line 


Condition use of ruby2_keywords to avoid introducing gem dependency


Drop superfluous newline
@jkowens
Copy link
Member

jkowens commented Mar 25, 2021

Looks good to me! Thanks @andrewtblake and @dentarg 👍

@jkowens jkowens requested a review from namusyaka March 25, 2021 14:36
alex-damian-negru pushed a commit to alex-damian-negru/rack-test that referenced this pull request Apr 6, 2021
Use sinatra PR for ruby-3 compatibility

sinatra/sinatra#1684

Remove sinatra

Bump bundler version

Bump ruby version

Temporarily disable rubocop/reek to focus on ruby-3
@namusyaka namusyaka merged commit ac5404d into sinatra:master Apr 17, 2021
namusyaka added a commit to namusyaka/sinatra that referenced this pull request Apr 17, 2021
Fix Delegator to pass keyword arguments for Ruby 3.0
namusyaka added a commit that referenced this pull request Apr 17, 2021
Fix Delegator to pass keyword arguments for Ruby 3.0
@dentarg dentarg mentioned this pull request Sep 22, 2021
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 this pull request may close these issues.

6 participants