Conversation
|
You're fast 😆 Technically, it's only if using Maybe if running in safe mode we should be more careful, so |
7ac8d16 to
38c767d
Compare
|
Oh, I couldn't catch it! I prefer being safe, so I updated this PR that way! For safe detection, it is limited to commonly used string and symbol comparisons when used |
There was a problem hiding this comment.
I think you forgot about filter, keep_if and delete_if. I don't use those Hash methods often, but I recall there were a ton of aliases there.
There was a problem hiding this comment.
I added filter method. On the other hand, delete_if and keep_if methods are not checked because they are incompatible with except method.
# except
h = {foo: 1, bar: 2, baz: 3}
=> {:foo=>1, :bar=>2, :baz=>3}
h.except(:bar)
=> {:foo=>1, :baz=>3}
h
=> {:foo=>1, :bar=>2, :baz=>3}
# delete_if
h = {foo: 1, bar: 2, baz: 3}
=> {:foo=>1, :bar=>2, :baz=>3}
h.delete_if {|k, v| k == :bar }
=> {:foo=>1, :baz=>3}
h
=> {:foo=>1, :baz=>3}
# keep_if
h = {foo: 1, bar: 2, baz: 3}
=> {:foo=>1, :bar=>2, :baz=>3}
h.keep_if {|k, v| k != :bar }
=> {:foo=>1, :baz=>3}
h
=> {:foo=>1, :baz=>3}I updated documentation and test on this.
38c767d to
678f147
Compare
This PR adds new `Style/HashExcept` cop.
This cop checks for usages of `Hash#reject`, `Hash#select`, and `Hash#filter` methods
that can be replaced with `Hash#except` method.
This cop should only be enabled on Ruby version 3.0 or higher.
(`Hash#except` was added in Ruby 3.0.)
```ruby
# bad
{foo: 1, bar: 2, baz: 3}.reject {|k, v| k == :bar }
{foo: 1, bar: 2, baz: 3}.select {|k, v| k != :bar }
# good
{foo: 1, bar: 2, baz: 3}.except(:bar)
```
cf. https://bugs.ruby-lang.org/issues/15822
678f147 to
6166df3
Compare
|
Thanks! |
|
@koic Question about this new cop. I've noticed this pulling through on ruby 2.7 - However it's pulling through as Just saw the ruby lang implementation for ruby 2.7 - So I'm just guessing this was in AS before that? |
This PR adds new
Style/HashExceptcop.This cop checks for usages of
Hash#rejectandHash#selectmethods that can be replaced withHash#exceptmethod.This cop should only be enabled on Ruby version 3.0 or higher.
(
Hash#exceptwas added in Ruby 3.0.)cf. https://bugs.ruby-lang.org/issues/15822
Before submitting the PR make sure the following are checked:
[Fix #issue-number](if the related issue exists).master(if not - rebase it).bundle exec rake default. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.mdif the new code introduces user-observable changes. See changelog entry format for details.