Skip to content

Commit 90946b6

Browse files
committed
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) ```
1 parent 0fdacef commit 90946b6

2 files changed

Lines changed: 2 additions & 1 deletion

File tree

lib/rake/task_arguments.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def fetch(*args, &block)
9595
end
9696

9797
def deconstruct_keys(keys)
98-
@hash.slice(*keys)
98+
keys ? @hash.slice(*keys) : to_hash
9999
end
100100

101101
protected

test/test_rake_task_arguments.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def test_deconstruct_keys
5858
omit "No stable pattern matching until Ruby 3.1 (testing #{RUBY_VERSION})" if RUBY_VERSION < "3.1"
5959

6060
ta = Rake::TaskArguments.new([:a, :b, :c], [1, 2, 3])
61+
assert_equal ta.deconstruct_keys(nil), { a: 1, b: 2, c: 3 }
6162
assert_equal ta.deconstruct_keys([:a, :b]), { a: 1, b: 2 }
6263
end
6364

0 commit comments

Comments
 (0)