How RuboCop Warns About the Format of Hoge[0,1]

Asked 5 months ago, Updated 5 months ago, 13 views

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.


2022-09-30 11:13

2 Answers

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:


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.

  - 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.

2022-09-30 11:13

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?

2022-09-30 11:13

If you have any answers or tips

© 2023 OneMinuteCode. All rights reserved.