Skip to content

Conversation

@ericproulx
Copy link
Contributor

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 **kwargs parameters.

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 **kwargs approach is more idiomatic and provides better clarity about method signatures.

Changes Made

Core API Changes

  • lib/grape/api.rb:

    • Updated override_all_methods! to use **kwargs instead of args.extract_options!
    • Modified add_setup to accept keyword arguments explicitly
    • Updated replay_step_on to handle keyword arguments with deep_transform_values
    • Enhanced skip_immediate_run? to check for lazy evaluation in keyword arguments
  • lib/grape/api/instance.rb:

    • Moved inherited method to private section for better encapsulation
    • Reorganized method order for improved readability

DSL Method Updates

  • lib/grape/dsl/inside_route.rb:

    • Updated present method to use **options instead of extract_options!
  • lib/grape/dsl/parameters.rb:

    • Modified use, requires, and optional methods to use explicit keyword arguments
    • Updated new_scope method signature to accept options as keyword arguments
  • lib/grape/dsl/request_response.rb:

    • Updated rescue_from method to use **options parameter
  • lib/grape/dsl/routing.rb:

    • Modified version method and all HTTP method definitions to use **options
    • Updated all route methods (get, post, put, etc.) to handle keyword arguments properly

Validation and Parameter Handling

  • lib/grape/validations/params_scope.rb:
    • Updated require_required_and_optional_fields to use keyword argument syntax
    • Modified new_scope method to accept options as keyword arguments
    • Updated field requirement methods to use **field_opts

Error Formatter and Params Builder

  • lib/grape/error_formatter/base.rb and lib/grape/params_builder/base.rb:
    • Moved inherited methods to private sections for better encapsulation

Dependencies

  • lib/grape.rb:
    • Removed unused active_support/core_ext/array/extract_options require
    • Added active_support/core_ext/hash/deep_transform_values require for handling nested keyword arguments

Test Updates

  • Updated all test files to use the new **kwargs syntax:
    • spec/grape/dsl/parameters_spec.rb
    • spec/grape/dsl/routing_spec.rb
    • spec/grape/validations/validators/except_values_validator_spec.rb
    • spec/shared/versioning_examples.rb

Technical Details

Key Improvements

  1. Explicit Keyword Arguments: All methods now explicitly declare keyword arguments using **kwargs instead of extracting them from a mixed argument list.

  2. Deep Transformation: Added support for deep_transform_values to handle nested keyword arguments that may contain lazy-evaluated values.

  3. Better Encapsulation: Moved inherited methods to private sections where appropriate.

  4. 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

  • All existing tests have been updated to use the new syntax
  • No breaking changes to the public API
  • Maintains full backward compatibility

Impact

  • ✅ Modernizes codebase for Ruby 3+ best practices
  • ✅ Improves code clarity with explicit keyword argument handling
  • ✅ Maintains full backward compatibility
  • ✅ No performance impact
  • ✅ All tests passing

Related Issues

This PR modernizes the Grape codebase to follow Ruby 3+ best practices for argument delegation, improving code maintainability and clarity.

@ericproulx ericproulx force-pushed the ruby3_handling_argument_delegation branch 2 times, most recently from ff24368 to bc84e5c Compare October 26, 2025 21:43
- Replace args.extract_options! with explicit **kwargs parameters - Update method signatures to use keyword arguments properly - Add deep_transform_values for handling nested keyword arguments - Move inherited methods to private sections where appropriate - Update tests to use **kwargs syntax - Remove unused active_support/core_ext/array/extract_options require This modernization improves code clarity and follows Ruby 3+ best practices while maintaining full backward compatibility.
@ericproulx ericproulx force-pushed the ruby3_handling_argument_delegation branch from bc84e5c to d42e837 Compare October 26, 2025 21:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant