How to prevent rubocop to add a space after hash - ruby

Rubocop changes #!/usr/bin/ruby to
# !/usr/bin/ruby
adds a space after the hash when I use rubocop -a, how can I avoid this

You'll have to disable the LeadingCommentSpace cop. It ensures that there is a space between # and the text that follows it. Puts something like this in your .rubocop.yml:
LeadingCommentSpace:
enabled: false
What you've stumbled upon is a bug, that I'll fix in the next RuboCop release (I'm its author). Obviously #! should be treated specially.

Related

Ruby frozen string comment syntax difference

There are two ways of enabling all strings in a file to be implicitly frozen1.
# frozen-string-literal: true
# frozen_string_literal: true
Is there a difference between these two syntaxes?
Thanks!
The answer you link to never uses the magic comment # frozen-string-literal: true only # frozen_string_literal: true. The difference is that only the latter will work.
The other way to enable frozen string literals is to run the application with the --enable=frozen-string-literal flag.

Why is rubocop asking me to put // around regex when i'm using %r already?

I have the following regex
regexp = %r{
((returned|undelivered)\smail|mail\sdelivery(\sfailed)?)
}x
But when I run rubocop on it, it complains that I need to "Use // around regular expression."
How can I get around it?
You can disable (and enable) any rubocop cop by adding a .rubocop.yml file to the root of your project folder and setting up the appropriate configurations. To see what you can do, check out the global default.yml in your rubocop package. It's fully commented.
For this particular problem, create a .rubocop.yml and...
To disable the cop completely:
Style/RegexpLiteral:
Enabled: false
To always use %r:
Style/RegexpLiteral:
EnforcedStyle: percent_r
I don't run rubocop so not sure this will solve your problem. You can use // instead of {} to surround the regex when using %r:
regexp = %r/((returned|undelivered)\smail|mail\sdelivery(\sfailed)?)/x
You can use multiline regexp with /.../x either:
regexp = /
((returned|undelivered)
\s
mail|mail
\s
delivery
(\sfailed)?)
/x
See more in Rubocop gem doc
when I run rubocop on it, it complains that I need to "Use // around regular expression."
How can I get around it?
I think the message is pretty clear: to get around it, you can use // around the regular expression:
regexp = /((returned|undelivered)\smail|mail\sdelivery(\sfailed)?)/x

Ruby: Why do I get warning "regex literal in condition" here?

A simple Ruby program, which works well (using Ruby 2.0.0):
#!/usr/bin/ruby
while gets
print if /foo/../bar/
end
However, Ruby also outputs the warning warning: regex literal in condition. It seems that Ruby considers my flip-flop-expression /foo/../bar/ as dangerous.
My question: Where lies the danger in this program? And: Can I turn off this warning (ideally only for this statement, keeping other warnings active)?
BTW, I found on the net several discussions of this kind of code, also mentioning the warning, but never found a good explanation why we get warned.
You can avoid the warning by using an explicit match:
while gets
print if ~/foo/..~/bar/
end
Regexp#~ matches against $_.
I don't know why the warning is shown (to be honest, I wasn't even aware that Ruby matches regexp literals against $_ implicitly), but according to the parser's source code, it is shown unless you provide the -e option when invoking Ruby, i.e. passing the script as an argument:
$ ruby -e "while gets; print if /foo/../bar/ end"
I would avoid using $_ as an implicit parameter and instead use something like:
while line = gets
print line if line=~/foo/..line=~/bar/
end
I think Neil Slater is right: It looks like a bug in a parser. If I change the code to
#!/usr/bin/ruby
while gets
print if $_=~/foo/..$_=~/bar/
end
the warning disappears.
I'll file a bug report.

Rubocop line length: How to ignore lines with comments?

I would like Rubocop to ignore lines with comments (just a comment or some code with an end of line comment) when checking if a line is too long. Is there a way to do this?
There is a way to ignore cops on a per line basis.
There is also a way to do it via configuration file.
Run rubocop --auto-gen-config and it will generate a file that you can use to disable the offenses.
The command also gives a hint on what to do to load those options.
On a line per line basis, you can enable and disable the cops as well.
# rubocop:disable RuleByName
This is a long line
# rubocop:enable RuleByName
You can also do more than one rule at a time in your code.
# rubocop:disable BlockComments, AsciiComments
By using an inline directive, the directive becomes valid only for that
line, and it would look like this:
# Thanks to #jnt30 for the comment!
method(argument) # rubocop:disable SomeRule, SomeOtherRule
You can read a ton more about RuboCop in its official manual.
To find all the rule names its worth looking in the rubocop config files
cyberwiz says - "run rubocop -D when I need the rule names rather than looking in the documentation." Update: This is now the default behavior without the flag.
The -D is now default, so we would get that for "free" now.
It's possible to define regex patterns to automatically ignore certain lines in rubocop.yml, so you could choose to ignore all lines starting with a # character:
Layout/LineLength:
Max: 80
AllowedPatterns: ['\A#']
This could be improved so that "indented" comment lines (i.e. whitespace followed by a # character) are also ignored, if that's what you want.
Note that this doesn't account for lines of code that end with a comment, though:
some_code(that_does_something) # This line would NOT be ignored by Rubocop.
You can use the following comment with rubocop to ignore a specific rule:
# rubocop:disable Layout/LineLength
def this_could_be_a_very_long_line_that_extends_forever_into_infinity
end
# rubocop:enable Layout/LineLength
You can also ignore whole files by adding them to .rubocop.yml:
AllCops:
Exclude:
- path/to/file.rb
i think the basic idea here is that you want to enforce line length, no matter what is after n characters. the default to 80 characters is some cargo cult for old terminal windows that could only hold that number of chars. the only option that i saw in the code is an option to allow urls that might exceed the character limit.
you can ignore whole files, i guess that's not what you are looking for.
The following configuration worked for me:
Layout/LineLength:
AllowedPatterns: ['^(\s*#)']
This regex only works when the entire line is commented out. Code followed by a long comment on the same line will still trigger a Rubocop lint error, which is by design.

Undoing auto-indentation

Wnen you use irb with auto indent mode, the end statements get indented one level extra
def foo
...
end
instead of showing the ordinary indenting convention:
def foo
...
end
because you cannot tell irb in advance that you are going to escape one level in the next line. This question has been addressed elsewhere like here or here, but neither gives a satisfactory answer. They just suggest giving up.
However, if we can minimally overwrite some irb methods so that auto indent will insert white spaces not in the prompt area but at the beginning of the line you type in, then by default, irb will still be inserting spaces, but we will be able to erase some spaces with backspace. Is this possible?
Or, if that is not realistic, then is it possible to make irb erase the last line from the screen and redisplay it with proper indentation right after you press Enter on a line including end?
Rewriting the last line is possible. Doing it in irb is difficult due to its lack of documentation and consistent api across versions. An irb alternative, ripl, has already solved this issue for itself with an auto-indent plugin. If you want to give ripl and its auto-indenting a try:
$ gem install ripl-auto_indent
$ echo "require 'ripl/auto_indent'" >> ~/.riplrc
# Auto-indent away
$ ripl
>> def foo
>> puts "it's auto-magic!"
>> end

Resources