-
Notifications
You must be signed in to change notification settings - Fork 624
Pattern matching support for arguments #515
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
Conversation
23fa4d4 to
bc17eee
Compare
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
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#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.)
| def deconstruct_keys(keys) | ||
| @hash.slice(*keys) | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should handle the nil case, too:
| def deconstruct_keys(keys) | |
| @hash.slice(*keys) | |
| end | |
| def deconstruct_keys(keys) | |
| keys ? @hash.slice(*keys) : @hash.dup | |
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
| 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 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should check the nil case, too:
| 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 } |
|
Great shout @nevans, thanks for spotting. I rarely use the |
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.