Skip to content

Prawn::View#method_missing drops keyword arguments on Ruby 3.x - #1284

Closed
Burgestrand wants to merge 1 commit into
prawnpdf:masterfrom
varvet:kbs/support-keyword-arguments-in-delegation
Closed

Prawn::View#method_missing drops keyword arguments on Ruby 3.x#1284
Burgestrand wants to merge 1 commit into
prawnpdf:masterfrom
varvet:kbs/support-keyword-arguments-in-delegation

Conversation

@Burgestrand

@Burgestrand Burgestrand commented Jan 26, 2023

Copy link
Copy Markdown
Contributor

Hi! This is my first message about this issue — I figured starting with a PR would make the issue more clear. I'm betting on CI running this code on older versions of Ruby to make sure this doesn't break anything by accident.

Ruby 3.x changes behavior of keyword arguments when used with single splat *arguments. Prawn::View#method_missing delegates missing methods to the document, but keyword arguments are dropped in the delegation in Ruby 3.

There aren't that many Prawn::Document methods that takes keyword arguments (I pretty much only found the gradient methods). We ran into this in our own code because we've added a few helper methods to Prawn::Document that does take keyword arguments, and we noticed in our upgrade to Ruby 3.x we couldn't use those methods.

In our own code we've temporarily fixed this with a monkey patch:

module Prawn::View
  ruby2_keywords :method_missing
end

Here's an example of behavior changed in Ruby 3.x vs 2.x:

def original(*args, **kwargs)
  p({ args: args, kwargs: kwargs })
end

def forward(*args)
  original(*args)
end

forward(1, 2, x: "x", y: "y")
# Ruby 2.7: {:args=>[1, 2], :kwargs=>{:x=>"x", :y=>"y"}}
# Ruby 3.x: {:args=>[1, 2, {:x=>"x", :y=>"y"}], :kwargs=>{}}

Come Ruby 3.x, keyword arguments will not be part of `*arguments` any more. We could either:

1. Mark the method with `ruby2_keywords`, which would make keyword arguments part of `*arguments` again. However, this is going away in the future so it's a crutch if anything.
2. Make the method properly support keyword arguments.

Option 2 is chosen here.
@Burgestrand Burgestrand changed the title Prawn::View#method_missing drops Ruby 3 keyword arguments when delegating Prawn::View#method_missing drops keyword arguments in Ruby 3.x Jan 26, 2023
@Burgestrand Burgestrand changed the title Prawn::View#method_missing drops keyword arguments in Ruby 3.x Prawn::View#method_missing drops keyword arguments on Ruby 3.x Jan 26, 2023
@pointlessone

Copy link
Copy Markdown
Member

@Burgestrand Thank you for your contribution. This was merged outside of GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants