From 90946b654c7e1d898d6cfb6ea563b4d5e6621d8a Mon Sep 17 00:00:00 2001 From: nick evans Date: Mon, 2 Jun 2025 10:54:49 -0400 Subject: [PATCH 1/2] Fix TaskArguments#deconstruct_keys with keys = nil `#deconstruct_keys` should handle `keys == nil`, or `**rest` will be broken. For example, the normal behavior looks like this: ```ruby {a: 1, b: 2, c: 3} => {a:, **rest} a # => 1 rest # => {b: 2, c: 3} ``` But without handling `keys == nil`, we'll get this: ```ruby class Example def initialize(hash) = @hash = hash def deconstruct_keys(keys) = @hash.slice(*keys) end Example.new({a: 1, b: 2, c: 3}) => {a:, **rest} # !> "#{inspect}: key not found: :a" (NoMatchingPatternKeyError) ``` --- lib/rake/task_arguments.rb | 2 +- test/test_rake_task_arguments.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/rake/task_arguments.rb b/lib/rake/task_arguments.rb index ecd27ab75..24abebcea 100644 --- a/lib/rake/task_arguments.rb +++ b/lib/rake/task_arguments.rb @@ -95,7 +95,7 @@ def fetch(*args, &block) end def deconstruct_keys(keys) - @hash.slice(*keys) + keys ? @hash.slice(*keys) : to_hash end protected diff --git a/test/test_rake_task_arguments.rb b/test/test_rake_task_arguments.rb index 2706827e5..d0d536fbb 100644 --- a/test/test_rake_task_arguments.rb +++ b/test/test_rake_task_arguments.rb @@ -58,6 +58,7 @@ def test_deconstruct_keys omit "No stable pattern matching until Ruby 3.1 (testing #{RUBY_VERSION})" if RUBY_VERSION < "3.1" ta = Rake::TaskArguments.new([:a, :b, :c], [1, 2, 3]) + assert_equal ta.deconstruct_keys(nil), { a: 1, b: 2, c: 3 } assert_equal ta.deconstruct_keys([:a, :b]), { a: 1, b: 2 } end From e8c48e07d861d7aac443e4bdf81e6ffca730be85 Mon Sep 17 00:00:00 2001 From: nick evans Date: Wed, 4 Jun 2025 15:23:04 -0400 Subject: [PATCH 2/2] Fix test assert expected/actual order Co-authored-by: Russell Garner --- test/test_rake_task_arguments.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_rake_task_arguments.rb b/test/test_rake_task_arguments.rb index d0d536fbb..371b4e737 100644 --- a/test/test_rake_task_arguments.rb +++ b/test/test_rake_task_arguments.rb @@ -58,8 +58,8 @@ def test_deconstruct_keys omit "No stable pattern matching until Ruby 3.1 (testing #{RUBY_VERSION})" if RUBY_VERSION < "3.1" ta = Rake::TaskArguments.new([:a, :b, :c], [1, 2, 3]) - assert_equal ta.deconstruct_keys(nil), { a: 1, b: 2, c: 3 } - assert_equal ta.deconstruct_keys([:a, :b]), { a: 1, b: 2 } + assert_equal({ a: 1, b: 2, c: 3 }, ta.deconstruct_keys(nil)) + assert_equal({ a: 1, b: 2 }, ta.deconstruct_keys([:a, :b])) end def test_enumerable_behavior