Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
Repo info
    Casper Christensen

    Hi. I have tried to search in the docs and on the web for a Rubocop rule to use <= and < instead of > or >= in comparison, example:

    # bad
    a > 2
    3 >= b
    2 < a
    b <= 3
    4 < c && c <= 7

    Does anyone know if this exist or not? Can't seem to find it.

    andre francois
    I have installed rubocop 0.62.0 in my rails application, but when I run "bundle exec rake new_cop[dept/name]"
    I get the error : "Don't know how to build task 'new_cop'"
    How do I create a new cop?
    Tejas Bubane
    @francoisa That rake task is for running inside the rubocop project directory itself. For contributing new cop.
    Brandon Weaver

    Bit of an out there idea I'd be interested in a sanity check on. Javascript has the idea of codemods, I kinda want to try and make the same thing in Ruby. Rubocop already has a lot of work done on top of parser, including the NodePattern syntax and autocorrect capabilities.

    Is there a possibility of generalizing the autocorrect capability into a code rewrite gem?

    This gem does some of that already - https://github.com/jonatas/fast
    Well, rather is there interest in doing that? I'd be willing to pitch in on it if so, but I'd rather not jump on an idea that's either already been done or isn't of interest to the contributors
    Hey all, would someone be able to address this issue for me? rubocop-hq/rubocop#6567 For me specifically the exclude in Allcops doesnt work at all. Wondering if it's the inheritance thats messing it up.
    Hi, I have a possibly dumb question.
    I've seen in the changelog people refer to their PR. How can I have the PR address before sending it?
    I mean, I'm working in my changeset, I then upload it to a branch on my fork and create a PR, but then I can't go back and change it to use the proper id afterwards.
    (I could in a different commit, after creating the PR, but I've seen it done in a single one)
    John Vandenberg
    @Oppen after you create the PR, you can add another commit to your branch and it will be included in the PR. You can also 'amend' (replace) the initial commit
    OK. I thought of amend, but that always carried weird consequences when I forced pushed.
    Hi all. I'm working in a performance related cop. Should I send it to rubocop, rubocop-performance or both?
    I have a open PR rubocop-hq/rubocop#6649, Could someone take a look at it and give me some feedback
    Lewis Jones
    Hi, I use Rubocop to write custom cops within my companies code bases to enforce our domain and help aid big refactors. I've got blocked trying to write cops that check if methods are called on instances of our domain object. Basically ruby classes we have created.
    For example I want to write a cop that creates an offence if anyone sends the method expiry_date to and instance of Quote object object.
    Can anyone point in the direction of an example of how to write a cop that can check the class of the object getting the method sent to it?
    Thomas Thomassen
    @LewisARJones_twitter The dynamic nature of Ruby makes this hard. I've tried to do something similar for rubocop-sketchup - but I found no good way to do so. In some cases I've scanned the parent scope to find the initialisation of the instance. But the moment the instance is passed on to other methods it gets really difficult to track. In the end I fell back on doing simple naive checks like mentioned. Or make educated guesses on variable names.
    You can see this being a challenge even for IDEs like RubyMine or VSCode with Ruby+Solargraph extensions. They tend to make use of YARD to pick up types from doc comments to aid the static analysic. But they also have to analyse the whole project to have chance. If you have good YARD comments you could try to leverage that in your custom cops.
    Jônatas Davi Paganini
    @LewisARJones_twitter any reason to not deprecate the usage of expiry_date in the Quote object? because it seems more reasonable than try to create a linter for it. Make the interface fail or warn developers about it. When they run tests they will know the api is no longer going to support it.
    Lewis Jones
    Thanks for your help @jonatas and @thomthom
    Mailo Světel
    Hey guys, I just discovered AllCops are being rewritten by children configs, what is the right way to do it?
    https://gitlab.com/lipoqil/rubocop-mailo/blob/459031ee5e101aa471dca90b202836f8d37025ca/rubocop.yaml#L1-7 If I use AllCops - Exclude in file which inherits from this one, the configs from gem are ignored 🥺
    Rubocop 0.65.0
    Philipp Hansch
    Hi, is there some way to write a cop that detects usage of :[] on Hashes and suggest fetch instead? Specifically, how could I limit this to only work on Hash?
    I suppose this is more difficult as I thought because of Ruby's dynamic nature..
    i.e, I'm currently getting false positives on Dir[pat], some_array[x], etc
    Jonas Arvidsson
    @phansch The short answer is "No". You'd have to add functionality in RuboCop to guess the type of values, similar to what "intellisense"/auto-completion does in IDEs.
    Colin Swenson-Healey
    Has anyone else recently updated rubocop, added the rubocop-performance gem and found that rubocop no longer inspects any of the files you had it inspecting before?
    It is inspecting only 2 files now when I run rubocop and I don't know which those are or why anything has changed
    Hey @chozandrias76 did you solve the issue? I have the same issue jumping from 47 -> 75
    I solved it by deleting the yml file and rewriting it line by line.

    Hi all,
    I'm having some difficulties finding a good solution to my current issue. In the project I work on (cucumber-ruby), I'm getting Rubocop errors on some string formatting. We have enforced annotated style for string formatting:

      EnforcedStyle: annotated

    but we also use formatting to specify number of decimals to keep in stone functions, eg:

    def format_duration(seconds)
      m, s = seconds.divmod(60)
      "#{m}m#{format('%.3f', s)}s"

    And of course, rubocop complains that this is not annotated style:

    Style/FormatStringToken: Prefer annotated tokens (like %<foo>s) over unannotated tokens (like %s).
            @io.print format_string(format('%.7f', stepdef_key.mean_duration), :skipped) + ' ' unless config.dry_run?

    How to solve this kind of problem ? (hopefully I'm on the right channel :) )

    ok, well after bit more research, that was pretty simple in fact: format('%<duration>.7f', duration: stepdef_key.mean_duration). I did not know the numbers formatting worked as the string ones :)
    CJ Avilla

    Anyone happen to have a link handy for where I might learn about how to initialize and run rubocop directly in Ruby? I'm trying to build something to check snippets of ruby in some documentation so the source file is a view template not a ruby file and contains other languages. I want to pass a string in Ruby to some actor and have it return cops or some such.

    For javascript, I'm doing something like

    Eslintrb.lint(raw_code, :eslintrc)

    I'll mostly be checking for Style cops

    Hi! I have a question.

    I have a rubocop.yml on Github, which I inherit_from in all my projects. Some projects are pure Ruby, some Rails, and some use RSpec.

    It is fine to require: rubocop-rails in a Rails project, naturally.

    But in a pure Ruby project I would not want to demand that the rubocop-rails gem is installed (neither should other developers have to install that gem, nor is it needed on CI).

    So, how would I organize my shared rubocop config?

    I cannot just specify any Rails/ cop in it, because that raises an error here

    Similarly, the RSpec cops should only run in projects that use RSpec.

    My idea is to have a rubocop.ruby.yml and a rubocop.rails.yml etc. on Github and inherit only the ones I need in my project.

    Is that the recommended solution? I thought it was nice to have only one yml file with all cops, but I don't see how I could achieve that without the disadvantages I tried to describe.

    Thank you very much.

    Max Schwenk

    curious why ruby-parse considers a block that has multiple lines to be nested within a begin. I know this isn't a rubocop question, just trying to figure out how one would generically say:

    this method call within a block of this type

    is it easier to do that or

    this block with this method call within

    create_table :a_table do |z|
      z.goober :name
      z.string :col
    create_table :a_table do |z|
      z.goober :name


        (send nil :create_table
          (sym :a_table))
            (arg :z)))
            (lvar :z) :goober
            (sym :name))
            (lvar :z) :string
            (sym :col))))
        (send nil :create_table
          (sym :a_table))
            (arg :z)))
          (lvar :z) :goober
          (sym :name))))

    @myii:matrix.org Hi, loving rubocop, we've deployed it across over 60 of our repos under https://github.com/saltstack-formulas. Found an issue with rubocop -a --safe, the first time it's actually caused a breakage. This is the section, before and after:

    Resulting in:

    test/integration/default/controls/config_spec.rb:158:35: F: Lint/Syntax: unterminated string meets end of file

    Worth a bug report?

    Michael Hagar

    Hi, I believe I found a bug that has possibly been fixed in a newer version of Rubocop. In version 0.57.2, this code:

    time_period = updated_during ? updated_during : 2.days.ago..Time.now

    is autocorrected to

    time_period = updated_during || 2.days.ago..Time.now

    However, that’s incorrect if updated_during is not nil, because || has a higher precedence than .., whereas ? has a lower precedence than ...

    Ankit Kumar
    how to configure rubocop in vs code, tried installing ruby-rubocop but throwing error env: jruby_executable_hooks: No such file or directory
    can someone help me to configure rubocop in vscode
    Jônatas Davi Paganini

    Hello friends! Is there any research or ongoing mechanism to allow us to use RuboCop combining autocorrect with user input confirmation?

    I'm asking because today a friend shared a script (https://gist.github.com/jaimerson/57847f25191f58018af06c1c1ea0e4f6) that autocorrects subject names from RSpec/NamedSubject and gives the opportunity to fix it ongoing or accept the suggestions. While cleaning up offenses it would speed up a lot the flow and we could have a default and allow people to edit it if they want through the interactive mode.

    It would also allow us to interactive fix cops that doesn't have autocorrect implementation because of some the dependency of the user input.


    Trying to find the Rubocop yml file. I looked in the .gem and .rvm folders.


    Andrew Jorgensen
    I have a rubocop question, hopefully this is the right place. I have a custom cop that compares class names to make sure there are no duplicates. The issue that i have is the class names are stored in a class level variable that gets cached in rubocop-daemon between runs so the first run is successful but the second one detects a duplicate class. Is there anything in rubocop that would allow me to clear that cache after the run or a run identifier I can make sure that names are compared within the same run and not across runs?
    The only other way i can think to do it without a before_run, after_run hook or an identifier is to time out the cache after a certain period of time
    Andrew Jorgensen
    It looks like the cops are created new for each source which makes cross file comparison difficult