Modernize argument delegation for Ruby 3+ compatibility #2618
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Update Argument Delegation for Ruby 3+ Compatibility
Overview
This PR updates the codebase to use modern Ruby 3+ argument delegation patterns by replacing
args.extract_options!with explicit keyword argument handling using**kwargsparameters.Background
Since Grape now requires Ruby >= 3.0, this PR modernizes the codebase to use Ruby 3's preferred argument delegation patterns. While
args.extract_options!still works in Ruby 3+, the explicit**kwargsapproach is more idiomatic and provides better clarity about method signatures.Changes Made
Core API Changes
lib/grape/api.rb:override_all_methods!to use**kwargsinstead ofargs.extract_options!add_setupto accept keyword arguments explicitlyreplay_step_onto handle keyword arguments withdeep_transform_valuesskip_immediate_run?to check for lazy evaluation in keyword argumentslib/grape/api/instance.rb:inheritedmethod to private section for better encapsulationDSL Method Updates
lib/grape/dsl/inside_route.rb:presentmethod to use**optionsinstead ofextract_options!lib/grape/dsl/parameters.rb:use,requires, andoptionalmethods to use explicit keyword argumentsnew_scopemethod signature to accept options as keyword argumentslib/grape/dsl/request_response.rb:rescue_frommethod to use**optionsparameterlib/grape/dsl/routing.rb:versionmethod and all HTTP method definitions to use**optionsValidation and Parameter Handling
lib/grape/validations/params_scope.rb:require_required_and_optional_fieldsto use keyword argument syntaxnew_scopemethod to accept options as keyword arguments**field_optsError Formatter and Params Builder
lib/grape/error_formatter/base.rbandlib/grape/params_builder/base.rb:inheritedmethods to private sections for better encapsulationDependencies
lib/grape.rb:active_support/core_ext/array/extract_optionsrequireactive_support/core_ext/hash/deep_transform_valuesrequire for handling nested keyword argumentsTest Updates
**kwargssyntax:spec/grape/dsl/parameters_spec.rbspec/grape/dsl/routing_spec.rbspec/grape/validations/validators/except_values_validator_spec.rbspec/shared/versioning_examples.rbTechnical Details
Key Improvements
Explicit Keyword Arguments: All methods now explicitly declare keyword arguments using
**kwargsinstead of extracting them from a mixed argument list.Deep Transformation: Added support for
deep_transform_valuesto handle nested keyword arguments that may contain lazy-evaluated values.Better Encapsulation: Moved
inheritedmethods to private sections where appropriate.Maintained Backward Compatibility: All changes maintain the same public API while fixing the underlying implementation.
Ruby 3+ Modernization
This PR modernizes the codebase to use Ruby 3+'s preferred argument handling patterns. The changes improve code clarity and follow current Ruby best practices while maintaining full compatibility with Ruby 3.0+.
Testing
Impact
Related Issues
This PR modernizes the Grape codebase to follow Ruby 3+ best practices for argument delegation, improving code maintainability and clarity.