Uh oh!
There was an error while loading. Please reload this page.
Pattern matching support for arguments - #515
Merged
Merged
Conversation
hsbtforce-pushed
the
task-arguments-deconstruct-keys
branch
from
March 15, 2024 00:40
23fa4d4 to
bc17eeeComparenobu
reviewed
Mar 29, 2024
Uh oh!
There was an error while loading. Please reload this page.
rgarnerforce-pushed
the
task-arguments-deconstruct-keys
branch
4 times, most recently
from
April 2, 2024 12:22
9ca8f32 to
cbd2e35CompareImplement `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.hsbtforce-pushed
the
task-arguments-deconstruct-keys
branch
from
May 30, 2025 05:49
cbd2e35 to
cff7664Comparenevans
reviewed
Jun 2, 2025
nevans
left a comment
Contributor
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# => 1rest# => {b: 2, c: 3}# without handling `keys == nil`classExampledefinitialize(hash)=@hash=hashdefdeconstruct_keys(keys)=@hash.slice(*keys)endExample.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
| defdeconstruct_keys(keys) | |
| @hash.slice(*keys) | |
| end | |
| defdeconstruct_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_equalta.deconstruct_keys([:a,:b]),{a: 1,b: 2} | |
| assert_equalta.deconstruct_keys(nil),{a: 1,b: 2,c: 3} | |
| assert_equalta.deconstruct_keys([:a,:b]),{a: 1,b: 2} |
nevans
commented
Jun 2, 2025
Contributor
rgarner
commented
Jun 2, 2025
ContributorAuthor
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 freeto 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.