diff --git a/lib/rake/task_arguments.rb b/lib/rake/task_arguments.rb index 0d3001afd..ecd27ab75 100644 --- a/lib/rake/task_arguments.rb +++ b/lib/rake/task_arguments.rb @@ -94,6 +94,10 @@ def fetch(*args, &block) @hash.fetch(*args, &block) end + def deconstruct_keys(keys) + @hash.slice(*keys) + end + protected def lookup(name) # :nodoc: diff --git a/test/test_rake_task_arguments.rb b/test/test_rake_task_arguments.rb index 245a71661..2706827e5 100644 --- a/test/test_rake_task_arguments.rb +++ b/test/test_rake_task_arguments.rb @@ -54,6 +54,13 @@ def test_to_hash assert_equal 0, h.fetch(:one) end + 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([:a, :b]), { a: 1, b: 2 } + end + def test_enumerable_behavior ta = Rake::TaskArguments.new([:a, :b, :c], [1, 2, 3]) assert_equal [10, 20, 30], ta.map { |k, v| v * 10 }.sort