Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pattern matching support for arguments #515

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/rake/task_arguments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions test/test_rake_task_arguments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ def test_to_hash
assert_equal 0, h.fetch(:one)
end

def test_deconstruct_keys
rgarner marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down