-
Notifications
You must be signed in to change notification settings - Fork 321
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
resolve #642 - Add Pattern Matching #643
base: main
Are you sure you want to change the base?
resolve #642 - Add Pattern Matching #643
Conversation
Resolves feature request httprb#642 requesting the addition of Pattern Matching hooks for Ruby 2.7+ by introducing `to_h`, `deconstruct`, and `deconstruct_keys` to core classes. This change also addresses spec changes by gating pattern matching specs on Ruby 2.7+ to prevent failures in older versions. All specs have also been given a pattern matching spec matching their implementation to demonstrate potential usages. While demonstrational usages could dive into nested classes that are in `HTTP` this was avoided to keep tests isolated from eachother.
It appears that conditional gating for Ruby 2.7+ isn't isolating these specs. I'll have to follow up with an additional commit some time tomorrow when I have time to address that. |
# @return [Hash[String, Symbol]] | ||
def underscored_keys_mapping | ||
Hash[keys.map { |k| [k, k.tr('A-Z-', 'a-z_').to_sym] }] | ||
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 may be unnecessary if an alternate method is available that does the same.
The only way I can get around "invalid" syntax that doesn't exist until 2.7 that I can think of or get to work is unfortunately very hacky: if RUBY_VERSION >= '2.7'
eval <<~RUBY
[1,2,3] in [1, *]
RUBY
end Would love to implement this for newer versions of Ruby but can't think of anything better than that hack for the moment |
d307135
to
d771e35
Compare
Resolves syntax errors introduced by pattern matching in older versions of Ruby through the use of `eval` to prevent immediate evaluation of invalid syntax.
d771e35
to
366767c
Compare
It appears that Ruby core uses a similar hack to get around old versions: |
Regarding CodeClimate I can reduce one of the methods out, but the other class not as easily. Granted my opinion is 20 methods / class is a bit arbitrary, but will defer to standards of the repo. |
Resolves feature request #642 requesting the addition of Pattern
Matching hooks for Ruby 2.7+ by introducing
to_h
,deconstruct
, anddeconstruct_keys
to core classes.This change also addresses spec changes by gating pattern matching specs
on Ruby 2.7+ to prevent failures in older versions. All specs have also
been given a pattern matching spec matching their implementation to
demonstrate potential usages.
While demonstrational usages could dive into nested classes that are in
HTTP
this was avoided to keep tests isolated from eachother.Examples of what this makes possible, among other things: