Pattern matching support for arguments#515
Merged
hsbt merged 1 commit intoruby:masterfrom May 30, 2025
Merged
Conversation
23fa4d4 to
bc17eee
Compare
nobu
reviewed
Mar 29, 2024
9ca8f32 to
cbd2e35
Compare
Implement `Rake::TaskArguments#deconstruct_keys` for use in Ruby 3.1
and up. This means in an idiomatic rake task we can use rightward
assignment to say:
```
task :get, %i[tenant id] do |_t, args|
args => {tenant:, id:}
...
end
```
... and omit the `.to_h` from `args`, raising `NoMatchingPatternError`
if either of the two params is absent from the task args.
cbd2e35 to
cff7664
Compare
nevans
reviewed
Jun 2, 2025
Contributor
nevans
left a comment
There was a problem hiding this comment.
#deconstruct_keys should handle keys == nil, or **rest will be broken.
# normal behavior:
{a: 1, b: 2, c: 3} => {a:, **rest}
a # => 1
rest # => {b: 2, c: 3}
# without handling `keys == nil`
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}
# !=> #<Example:0x00007479b6b781b8 @hash={a: 1, b: 2, c: 3}>: key not found: :a (NoMatchingPatternKeyError)(My apologies for not creating a PR... I can do that later today.)
Comment on lines
+97
to
+99
| def deconstruct_keys(keys) | ||
| @hash.slice(*keys) | ||
| end |
Contributor
There was a problem hiding this comment.
This should handle the nil case, too:
Suggested change
| def deconstruct_keys(keys) | |
| @hash.slice(*keys) | |
| end | |
| def deconstruct_keys(keys) | |
| keys ? @hash.slice(*keys) : @hash.dup | |
| end |
| 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 } |
Contributor
There was a problem hiding this comment.
This should check the nil case, too:
Suggested change
| assert_equal ta.deconstruct_keys([:a, :b]), { a: 1, b: 2 } | |
| assert_equal ta.deconstruct_keys(nil), { a: 1, b: 2, c: 3 } | |
| assert_equal ta.deconstruct_keys([:a, :b]), { a: 1, b: 2 } |
Contributor
Contributor
Author
|
Great shout @nevans, thanks for spotting. I rarely use the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implement
Rake::TaskArguments#deconstruct_keys. This means in an idiomatic ruby 3.x rake task we can use rightward assignment to say:... and omit the
.to_hfromargs, raisingNoMatchingPatternErrorif either of the two params is absent from the task args.