Note: RuboCop|Style/FormatString-Qiita
As you can see in the reference link, there is a way to print the message to stop using %
in RuboCop and use format
.
In the same way, can I stop using hoge[1,2]
in the array and use hoge.slices(1,2)
?
Because it is easy to mix with values_at
, I think issuing a warning to use slice will make it easier for you to notice that you used it as values_at
.
As the person who answered earlier said, there is no such Cop (Cop, by the way, is a unit of rules in RuboCop).
Also, I think there will be a lot of false positive warnings, so even if you create such a Cop, RuboCop will not include it.
For example, the String class also defines the []
method:
https://docs.ruby-lang.org/ja/latest/class/String.html#I_--5B--5D
x="hoge"
x[1,2]#=>"og"
Therefore, simply looking at the code, hoge[1,2]
cannot distinguish whether it is a method call to Array or a String, which causes false positives.
So why don't you use another tool instead of RuboCop?
For example, with a tool called query, you can easily define these rules yourself.
https://github.com/soutaro/querly
#query.yaml
rules:
- id —array.slice
pattern: "[](_,_)"
message: 'Please use array.slice(x,y) instead of array[x,y]'
before: 'array [x,y]'
after: 'array.slice(x,y)'
You can save the above information as query.yaml
and run the command query check.
to perform the expected inspection.
This tool is distributed in Rubygems and can be installed as gem install query
.
If there was a Cop like that, I looked it up based on the prediction that searching repository on slice would be part of the indicated message and found it missing.
So I don't think so at the moment, so should I make a proposal in Issue or write a copy myself?
© 2024 OneMinuteCode. All rights reserved.