EzDevInfo.com

rubocop

A Ruby static code analyzer, based on the community Ruby style guide.

How to avoid auto correction of numbers in rubocop

How to avoid auto correction of numbers in rubocop. I don't think the following is correct.

-        (Time.now.to_i * 10000).to_s
+        (Time.now.to_i * 10_000).to_s

Source: (StackOverflow)

Why is a method with too many lines a bad thing? [closed]

In my rails app, I have a method like this:

def cart
    if user_signed_in?
        @user = current_user
        if @user.cart.present?
            @cart = @user.cart
        else
            @cart = false
        end
    else
        cart_id = session[:cart_id]

        if cart_id.present?
            @cart = Cart.find(cart_id.to_i)
        else
            @cart = false
        end
    end
end

Rubocop flagged this method as Method had too many lines. Why is it bad to write a method with too many lines? What if we have to do a lot of work in it? How can I re-factor this and write good code?


Source: (StackOverflow)

Advertisements

Use \ instead of + or << to concatenate those strings

I'm using rubocop to basically clean up my messy code...

One of the errors were:

Use \ instead of + or << to concatenate those strings.

Why is this?

I can't find it in the Ruby documentation. Why would I use \ instead of + or <<?


Source: (StackOverflow)

Why does rubocop or the ruby style guide prefer not to use get_ or set_?

I was running rubocop on my project and fixing the complaints it raised.

One particular complaint bothered me

Do not prefix reader method names with get_

I could not understand much from this complaint so I looked at source code in github.

I found this snippet

    def bad_reader_name?(method_name, args)
      method_name.start_with?('get_') && args.to_a.empty?
    end

    def bad_writer_name?(method_name, args)
      method_name.start_with?('set_') && args.to_a.one?
    end

So the advice or convention is as follows:

1) Actually they advice us not to use get_ when the method does not have arguments . otherwise they allow get_

2) And they advice us not to use set_ when the method has only one argument .otherwise they allow set_

What is the reason behind this convention or rule or advice?


Source: (StackOverflow)

Rubocop rule to enforce double indent when assignment goes on next line

Our coding style says that if an assignment doesn't fit on one line, but the assigned value will fit on a line by itself, we should indent that line by four spaces instead of two. Example:

my_var = Some.reasonable_method_call(param1)
my_var2 = 
    Some.crazy_long_ridiculous_method_that_doesnt_fit_on_same_line(param1)

Is there a Rubocop rule I can use to enforce this? We use a two-space indent inside a block, and the four-space indent for long assignments helps indicate visually that that line is not nested in a block but rather a continuation of the previous line.


Source: (StackOverflow)

How to make rubocop output nothing when there are no warnings or offenses?

I'd like rubocop but have it create no output if there are no offenses. I've played with the documented command line options, and taken a quick peek a the config option and source code without seeing anything promising.

The closest I've come is rubocop -f o, but even that produces a summary line:

$ rubocop -f o

--
0  Total

Any ideas on how to suppress output on a clean, successful run?


Source: (StackOverflow)

Ruby tool to reformat array literals [closed]

Is there a Ruby tool or editor plugin that will convert an array literal into a %w array literal?

ARRAY = [
  "Foo",
  "Bar",
  "Baz"
]

ARRAY = %w(
  Foo
  Bar
  Baz
)

I know I could write my own regex to do this, but I'm hoping for something off-the-shelf. I'm already using Rubocop as a linter, but it doesn't cover it. I can't seem to find a plugin for RubyMine, Sublime, or Atom for it either.


Source: (StackOverflow)

Several file extensions in VIM autocmd

I got autocmd BufWrite *.rb :RuboCop -a in my ~/.vimrc

How to modify it to process *.jbuilder files as well?


Source: (StackOverflow)

How to pass &:key as an argument to map instead of a block with ruby?

I wrote this code:

my.objects.map { |object| object.key }

My rubocop said:

Pass &:key as an argument to map instead of a block.

Is there a short way to do the same thing?


Source: (StackOverflow)

How to make RuboCop inspect a file in a folder that starts with dot

I want to run RuboCop for VisualEditor repository. At the moment, the only Ruby file I could find in the repository is .docs/CustomTags.rb.

$ find . | grep rb
./.docs/CustomTags.rb

If I run only rubocop, it does not find any files:

$ rubocop
Inspecting 0 files
0 files inspected, no offenses detected

I guess it ignores files in folders that start with dot (.docs).

RuboCop documentation on including files says:

If you'd like it to check other files you'll need to manually pass them in, or to add entries for them under AllCops/Include.

If I provide path to the file from the command line, RuboCop finds the file:

$ rubocop .docs/CustomTags.rb 
Inspecting 1 file
W
(...)
1 file inspected, 26 offenses detected

Our continuous integration just runs rubocop for the repository, so I can not provide path to the file from the command line. I have to use AllCops/Include, but I can not figure out how to do it.

If I create a .rubocop.yml in the root of the repository:

AllCops:
  Include:
    - '.docs/CustomTags.rb'

and run Rubocop, it does not find the file:

$ rubocop
Inspecting 0 files
0 files inspected, no offenses detected

I have tried several variations of the .rubocop.yml file, including:

AllCops:
  Include:
    - '**/CustomTags.rb'

and

AllCops:
  Include:
    - '.docs/**/*'

But none of them are finding the file.


Source: (StackOverflow)

Rails - Rake test and rubocop in one task

I'm trying to setup my rails project so that all the verification required by a contributor is in one command, currently we have been running:

rake test

But now we also want to use rubocop for static analysis:

rubocop -R -a

I want this to be executable in one simple rake task. It would be nice to override 'rake test' to run rubocop then the standard rake test stuff for a rails project, as then no-one will have to remember to change the command. But if I have to create a separate rake task, that's probably fine too.

I've seen the rubocop rake integration here, at the bottom, but I'm not sure how to bundle that with 'rake test' into one task... Any thoughts?


Source: (StackOverflow)

Tightening Up Ruby Code, Making Small Methods

I have a method that is reported by RuboCop as too long: we are only allowed seven lines per method. Here's the offending method:

def on(definition, visit = false, &block)
  if @active.is_a?(definition)
    block.call @active if block
    return @active
  end

  @active = definition.new
  @active.load if visit

  block.call @active if block

  @active
end

I was going to convert the top if condition to a guard clause, but I don't see how to do that.

I tried combining lines 7 and 8 into this:

@active = definition.new().load if visit

but that most definitely does not work.

I can't leave RuboCop violations active nor can I change the tolerances.


Source: (StackOverflow)

How to ignore rubocop check when using guard-rspec?

When I add a guard-rspec gem and set the Guardfile as this:

guard :rspec, cmd: 'bundle exec rspec' do
  watch('spec/spec_helper.rb')                        { "spec" }
  watch('config/routes.rb')                           { "spec/routing" }
  watch('app/controllers/application_controller.rb')  { "spec/controllers" }
  watch(%r{^spec/.+_spec\.rb$})
end

Then run rubocop to check:

rake rubocop:rubocop

It showed:

Guardfile:5:9: C: Use %r only for regular expressions matching more than 1 '/' character.
watch(%r{^spec/.+_spec\.rb$})
      ^^^^^^^^^^^^^^^^^^^^^^

Should I try to find a way to rewrite the regular code or write a ignore code to .rubocop.yml file?


Source: (StackOverflow)

Set config file on Rubocop Sublime Linter

I'm using Rubocop alongside SublimeLinter and we also included it on our continuous integration server, so there is a rubocop.yml on the root of our project.

The file in the root is the one the CI uses to validate the code, but it is not as strict as I want it to be in Sublime.

Is there a way to specify the yml file that I want Rubocop to use in Sublime?


Source: (StackOverflow)

Make Rails generate single quote strings rather than double quote strings to satisfy RuboCop

How can I make Rails generate single quoted strings rather than double quoted strings when it generates code, such as migrations?

I'm not too fussed about what kind of strings are used in migrations, but it causes complications for RuboCop. The only way I can envisage RuboCop ignoring it is if I explicitly tell it to ignore the offending files, or to not enforce the Style/StringLiterals cop at all.


Source: (StackOverflow)