diff --git a/rubocop/examples/bad/Style_AccessModifierIndentation.rb b/rubocop/examples/bad/Style_AccessModifierIndentation.rb new file mode 100644 index 0000000..c33d3a3 --- /dev/null +++ b/rubocop/examples/bad/Style_AccessModifierIndentation.rb @@ -0,0 +1,20 @@ +# 'https://github.com/bbatsov/ruby-style-guide#indent-public-private-protected' + +# Indent the public, protected, and private methods as much as the method definitions they apply to. +# Leave one blank line above the visibility modifier and one blank line below in order to emphasize that it applies to all methods below it + +# bad +class SomeClass + def public_method + # ... + end + + private + def private_method + # ... + end + + def another_private_method + # ... + end +end diff --git a/rubocop/examples/bad/Style_Alias.rb b/rubocop/examples/bad/Style_Alias.rb new file mode 100644 index 0000000..e6161a9 --- /dev/null +++ b/rubocop/examples/bad/Style_Alias.rb @@ -0,0 +1,5 @@ +# 'https://github.com/bbatsov/ruby-style-guide#alias-method' + +# TODO: Always use alias_method when aliasing methods of modules, classes, +# or singleton classes at runtime as the lexical scope of alias leads +# to unpredictability in these cases. diff --git a/rubocop/examples/bad/Style_AlignHash.rb b/rubocop/examples/bad/Style_AlignHash.rb new file mode 100644 index 0000000..3b367ca --- /dev/null +++ b/rubocop/examples/bad/Style_AlignHash.rb @@ -0,0 +1,2 @@ +# TODO: Here we check if the keys, separators, +# and values of a multi-line hash literal are aligned. diff --git a/rubocop/examples/bad/Style_AlignParameters.rb b/rubocop/examples/bad/Style_AlignParameters.rb new file mode 100644 index 0000000..ffa263a --- /dev/null +++ b/rubocop/examples/bad/Style_AlignParameters.rb @@ -0,0 +1,20 @@ +# https://github.com/bbatsov/ruby-style-guide#no-double-indent + +# Align the parameters of a method call if they span more than one line. +# When aligning parameters is not appropriate due to line-length constraints, +# single indent for the lines after the first is also acceptable. + +# starting point (line is too long) +def send_mail(source) + Mailer.deliver(to: 'bob@example.com', from: 'us@example.com', + subject: 'Important message', body: source.text) +end + +# bad (double indent) +def send_mail(source) + Mailer.deliver( + to: 'bob@example.com', + from: 'us@example.com', + subject: 'Important message', + body: source.text) +end diff --git a/rubocop/examples/bad/Style_AndOr.rb b/rubocop/examples/bad/Style_AndOr.rb new file mode 100644 index 0000000..4426978 --- /dev/null +++ b/rubocop/examples/bad/Style_AndOr.rb @@ -0,0 +1,13 @@ +# 'https://github.com/bbatsov/ruby-style-guide#no-and-or-or' + +# The and and or keywords are banned. +# It's just not worth it. Always use && and || instead. + +# bad +# boolean expression +if some_condition and some_other_condition + do_something +end + +# control flow +document.saved? or document.save! diff --git a/rubocop/examples/bad/Style_ArrayJoin.rb b/rubocop/examples/bad/Style_ArrayJoin.rb new file mode 100644 index 0000000..a7f0252 --- /dev/null +++ b/rubocop/examples/bad/Style_ArrayJoin.rb @@ -0,0 +1,8 @@ +# 'https://github.com/bbatsov/ruby-style-guide#array-join' + +# Favor the use of Array#join over the fairly cryptic +# Array#* with a string argument. + +# bad +%w(one two three) * ', ' +# => 'one, two, three' diff --git a/rubocop/examples/bad/Style_AsciiIdentifiers.rb b/rubocop/examples/bad/Style_AsciiIdentifiers.rb new file mode 100644 index 0000000..e67ef76 --- /dev/null +++ b/rubocop/examples/bad/Style_AsciiIdentifiers.rb @@ -0,0 +1,9 @@ +# 'https://github.com/bbatsov/ruby-style-guide#english-identifiers' + +# Name identifiers in English. + +# bad - identifier using non-ascii characters +заплата = 1_000 + +# bad - identifier is a Bulgarian word, written with Latin letters (instead of Cyrillic) +zaplata = 1_000 diff --git a/rubocop/examples/bad/Style_Attr.rb b/rubocop/examples/bad/Style_Attr.rb new file mode 100644 index 0000000..1dbdadd --- /dev/null +++ b/rubocop/examples/bad/Style_Attr.rb @@ -0,0 +1,7 @@ +# 'https://github.com/bbatsov/ruby-style-guide#attr' + +# Avoid the use of attr. Use attr_reader and attr_accessor instead. + +# bad - creates a single attribute accessor +attr :something, true +attr :one, :two, :three # behaves as attr_reader diff --git a/rubocop/examples/bad/Style_BeginBlock.rb b/rubocop/examples/bad/Style_BeginBlock.rb new file mode 100644 index 0000000..d3a00b5 --- /dev/null +++ b/rubocop/examples/bad/Style_BeginBlock.rb @@ -0,0 +1,3 @@ +# 'https://github.com/bbatsov/ruby-style-guide#no-BEGIN-blocks' + +#TODO: Avoid the use of BEGIN blocks. diff --git a/rubocop/examples/bad/Style_BlockComments.rb b/rubocop/examples/bad/Style_BlockComments.rb new file mode 100644 index 0000000..f22cc8b --- /dev/null +++ b/rubocop/examples/bad/Style_BlockComments.rb @@ -0,0 +1,10 @@ +# 'https://github.com/bbatsov/ruby-style-guide#no-block-comments' + +# Don't use block comments. +# They cannot be preceded by whitespace and are not as easy to spot as regular comments + +# bad +=begin +comment line +another comment line +=end diff --git a/rubocop/examples/bad/Style_BlockDelimiters.rb b/rubocop/examples/bad/Style_BlockDelimiters.rb new file mode 100644 index 0000000..1b51f44 --- /dev/null +++ b/rubocop/examples/bad/Style_BlockDelimiters.rb @@ -0,0 +1,18 @@ +# 'https://github.com/bbatsov/ruby-style-guide#single-line-blocks' + +# Prefer {...} over do...end for single-line blocks. +# Avoid using {...} for multi-line blocks (multiline chaining is always ugly). +# Always use do...end for "control flow" and "method definitions" (e.g. +# in Rakefiles and certain DSLs). Avoid do...end when chaining. + +names = %w(Bozhidar Steve Sarah) + +# bad +names.each do |name| + puts name +end + +# bad +names.select do |name| + name.start_with?('S') +end.map { |name| name.upcase } diff --git a/rubocop/examples/bad/Style_BlockNesting.rb b/rubocop/examples/bad/Style_BlockNesting.rb new file mode 100644 index 0000000..1762597 --- /dev/null +++ b/rubocop/examples/bad/Style_BlockNesting.rb @@ -0,0 +1,5 @@ +# TODO: This cop checks for excessive nesting of conditional +# and looping constructs. Despite the cop's name, +# blocks are not considered as an extra level of nesting. + +# TODO: The maximum level of nesting allowed is configurable. diff --git a/rubocop/examples/bad/Style_BracesAroundHashParameters.rb b/rubocop/examples/bad/Style_BracesAroundHashParameters.rb new file mode 100644 index 0000000..9aabc91 --- /dev/null +++ b/rubocop/examples/bad/Style_BracesAroundHashParameters.rb @@ -0,0 +1,2 @@ +# TODO: This cop checks for braces around the last parameter +# in a method call if the last parameter is a hash. diff --git a/rubocop/examples/bad/Style_CaseEquality.rb b/rubocop/examples/bad/Style_CaseEquality.rb new file mode 100644 index 0000000..c70acb6 --- /dev/null +++ b/rubocop/examples/bad/Style_CaseEquality.rb @@ -0,0 +1,10 @@ +# 'https://github.com/bbatsov/ruby-style-guide#no-case-equality' + +# Avoid explicit use of the case equality operator ===. +# As its name implies it is meant to be used implicitly by case expressions +# and outside of them it yields some pretty confusing code. + +# bad +Array === something +(1..100) === 7 +/something/ === some_string diff --git a/rubocop/examples/bad/Style_CaseIndentation.rb b/rubocop/examples/bad/Style_CaseIndentation.rb new file mode 100644 index 0000000..1bce472 --- /dev/null +++ b/rubocop/examples/bad/Style_CaseIndentation.rb @@ -0,0 +1,17 @@ +# 'https://github.com/bbatsov/ruby-style-guide#indent-when-to-case' + +# Indent when as deep as case. +# This is the style established in both "The Ruby Programming Language" +# and "Programming Ruby". + +# bad +case + when song.name == 'Misty' + puts 'Not again!' + when song.duration > 120 + puts 'Too long!' + when Time.now.hour > 21 + puts "It's too late" + else + song.play +end diff --git a/rubocop/examples/bad/Style_CharacterLiteral.rb b/rubocop/examples/bad/Style_CharacterLiteral.rb new file mode 100644 index 0000000..741ba7f --- /dev/null +++ b/rubocop/examples/bad/Style_CharacterLiteral.rb @@ -0,0 +1,8 @@ +# 'https://github.com/bbatsov/ruby-style-guide#no-character-literals' + +# Don't use the character literal syntax ?x. +# Since Ruby 1.9 it's basically redundant - ?x would interpreted as 'x' +# (a string with a single character in it). + +# bad +char = ?c diff --git a/rubocop/examples/bad/Style_ClassAndModuleCamelCase.rb b/rubocop/examples/bad/Style_ClassAndModuleCamelCase.rb new file mode 100644 index 0000000..c6b5aa0 --- /dev/null +++ b/rubocop/examples/bad/Style_ClassAndModuleCamelCase.rb @@ -0,0 +1,20 @@ +# 'https://github.com/bbatsov/ruby-style-guide#camelcase-classes' + +# Use CamelCase for classes and modules. + +# bad +class Someclass + ... +end + +class Some_Class + ... +end + +class SomeXml + ... +end + +class XmlSomething + ... +end diff --git a/rubocop/examples/bad/Style_ClassAndModuleChildren.rb b/rubocop/examples/bad/Style_ClassAndModuleChildren.rb new file mode 100644 index 0000000..d4787c1 --- /dev/null +++ b/rubocop/examples/bad/Style_ClassAndModuleChildren.rb @@ -0,0 +1,9 @@ +# TODO:This cop checks the style of children definitions at classes and modules. +# Basically there are two different styles: +# nested - have each child on its own line + +# bad +class Foo + class Bar + end +end diff --git a/rubocop/examples/bad/Style_ClassCheck.rb b/rubocop/examples/bad/Style_ClassCheck.rb new file mode 100644 index 0000000..eb70060 --- /dev/null +++ b/rubocop/examples/bad/Style_ClassCheck.rb @@ -0,0 +1 @@ +# TODO: This cop enforces consistent use of `Object#is_a?` or `Object#kind_of?`. diff --git a/rubocop/examples/bad/Style_ClassMethods.rb b/rubocop/examples/bad/Style_ClassMethods.rb new file mode 100644 index 0000000..9d35c35 --- /dev/null +++ b/rubocop/examples/bad/Style_ClassMethods.rb @@ -0,0 +1,23 @@ +StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#def-self-class-methods' + +# Use def self.method to define class methods. +# This makes the code easier to refactor since the class name is not repeated. + +class TestClass + # bad + def TestClass.some_method + # body omitted + end + + # Also possible and convenient when you + # have to define many class methods. + class << self + def first_method + # body omitted + end + + def second_method_etc + # body omitted + end + end +end diff --git a/rubocop/examples/bad/Style_ClassVars.rb b/rubocop/examples/bad/Style_ClassVars.rb new file mode 100644 index 0000000..2b312eb --- /dev/null +++ b/rubocop/examples/bad/Style_ClassVars.rb @@ -0,0 +1,8 @@ +# 'https://github.com/bbatsov/ruby-style-guide#no-class-vars' + +# TODO: Avoid the usage of class (@@) variables due to their "nasty" +# behavior in inheritance. + +# As you can see all the classes in a class hierarchy actually +# share one class variable. +# Class instance variables should usually be preferred over class variables. diff --git a/rubocop/examples/bad/Style_CollectionMethods.rb b/rubocop/examples/bad/Style_CollectionMethods.rb new file mode 100644 index 0000000..1de2f98 --- /dev/null +++ b/rubocop/examples/bad/Style_CollectionMethods.rb @@ -0,0 +1,5 @@ +# TODO: This cop checks for uses of unidiomatic method +# names from the Enumerable module. + +# TODO: The current definition of the check is flawed and should be enhanced +# by check for by blocks & procs as arguments of the methods. diff --git a/rubocop/examples/bad/Style_ColonMethodCall.rb b/rubocop/examples/bad/Style_ColonMethodCall.rb new file mode 100644 index 0000000..da3d407 --- /dev/null +++ b/rubocop/examples/bad/Style_ColonMethodCall.rb @@ -0,0 +1,9 @@ +# 'https://github.com/bbatsov/ruby-style-guide#double-colons' + +# Use :: only to reference constants(this includes classes and modules) and +# constructors (like Array() or Nokogiri::HTML()). +# Do not use :: for regular method invocation. + +# bad +SomeClass::some_method +some_object::some_method diff --git a/rubocop/examples/bad/Style_CommentAnnotation.rb b/rubocop/examples/bad/Style_CommentAnnotation.rb new file mode 100644 index 0000000..892cf58 --- /dev/null +++ b/rubocop/examples/bad/Style_CommentAnnotation.rb @@ -0,0 +1,4 @@ +# 'https://github.com/bbatsov/ruby-style-guide#annotate-keywords' + +# TODO:The annotation keyword is followed by a colon and a space, +# then a note describing the problem. diff --git a/rubocop/examples/bad/Style_CommentIndentation.rb b/rubocop/examples/bad/Style_CommentIndentation.rb new file mode 100644 index 0000000..62d8329 --- /dev/null +++ b/rubocop/examples/bad/Style_CommentIndentation.rb @@ -0,0 +1 @@ +# TODO: This cops checks the indentation of comments. diff --git a/rubocop/examples/bad/Style_ConstantName.rb b/rubocop/examples/bad/Style_ConstantName.rb new file mode 100644 index 0000000..1b3acbc --- /dev/null +++ b/rubocop/examples/bad/Style_ConstantName.rb @@ -0,0 +1,6 @@ +# 'https://github.com/bbatsov/ruby-style-guide#screaming-snake-case' + +# Use SCREAMING_SNAKE_CASE for other constants. + +# bad +SomeConst = 5 diff --git a/rubocop/examples/bad/Style_DefWithParentheses.rb b/rubocop/examples/bad/Style_DefWithParentheses.rb new file mode 100644 index 0000000..4b1e6fc --- /dev/null +++ b/rubocop/examples/bad/Style_DefWithParentheses.rb @@ -0,0 +1,15 @@ +# 'https://github.com/bbatsov/ruby-style-guide#method-parens' + +# Use def with parentheses when there are parameters. +# Omit the parentheses when the method doesn't accept any parameters. + +# bad +def some_method() + # body omitted +end + +# bad +def some_method_with_parameters param1, param2 + # body omitted +end + diff --git a/rubocop/examples/bad/Style_DeprecatedHashMethods.rb b/rubocop/examples/bad/Style_DeprecatedHashMethods.rb new file mode 100644 index 0000000..a7ba9d4 --- /dev/null +++ b/rubocop/examples/bad/Style_DeprecatedHashMethods.rb @@ -0,0 +1,9 @@ +# 'https://github.com/bbatsov/ruby-style-guide#hash-key' + +# Use Hash#key? instead of Hash#has_key? and Hash#value? +# instead of Hash#has_value?. As noted here by Matz, +# the longer forms are considered deprecated. + +# bad +hash.has_key?(:test) +hash.has_value?(value) diff --git a/rubocop/examples/bad/Style_Documentation.rb b/rubocop/examples/bad/Style_Documentation.rb new file mode 100644 index 0000000..db3c533 --- /dev/null +++ b/rubocop/examples/bad/Style_Documentation.rb @@ -0,0 +1,4 @@ +# TODO: This cop checks for missing top-level documentation +# of classes and modules. Classes with no body are exempt from the check and +# so are namespace modules - modules that have nothing +# in their bodies except classes or other other modules. diff --git a/rubocop/examples/bad/Style_DotPosition.rb b/rubocop/examples/bad/Style_DotPosition.rb new file mode 100644 index 0000000..22f5f3c --- /dev/null +++ b/rubocop/examples/bad/Style_DotPosition.rb @@ -0,0 +1,20 @@ +# 'https://github.com/bbatsov/ruby-style-guide#consistent-multi-line-chains' + +# Adopt a consistent multi-line method chaining style. +# There are two popular styles in the Ruby community, +# both of which are considered good - leading . (Option A) and +# trailing . (Option B). + +# (Option A) When continuing a chained method invocation +# on another line keep the . on the second line. + +# bad - need to consult first line to understand second line +one.two.three. + four + +# (Option B) When continuing a chained method invocation on another line, +# include the . on the first line to indicate that the expression continues. + +# bad - need to read ahead to the second line to know that the chain continues +one.two.three + .four diff --git a/rubocop/examples/bad/Style_SelfAssignment.rb b/rubocop/examples/bad/Style_SelfAssignment.rb new file mode 100644 index 0000000..db1f86e --- /dev/null +++ b/rubocop/examples/bad/Style_SelfAssignment.rb @@ -0,0 +1,9 @@ +# Use shorthand self assignment operators whenever applicable. + +# bad +x = x + y +x = x * y +x = x**y +x = x / y +x = x || y +x = x && y diff --git a/rubocop/examples/bad/Style_Semicolon.rb b/rubocop/examples/bad/Style_Semicolon.rb new file mode 100644 index 0000000..966fcd7 --- /dev/null +++ b/rubocop/examples/bad/Style_Semicolon.rb @@ -0,0 +1,7 @@ +# Don't use ; to separate statements and expressions. +# As a corollary - use one expression per line. + +# bad +puts 'foobar'; # superfluous semicolon + +puts 'foo'; puts 'bar' # two expressions on the same line diff --git a/rubocop/examples/bad/Style_SignalException.rb b/rubocop/examples/bad/Style_SignalException.rb new file mode 100644 index 0000000..65fdce6 --- /dev/null +++ b/rubocop/examples/bad/Style_SignalException.rb @@ -0,0 +1,4 @@ +# TODO: This cop checks for uses of `fail` and `raise`. + +# TODO: 'Use `fail` instead of `raise` to signal exceptions.' +# TODO: 'Use `raise` instead of `fail` to rethrow exceptions.' diff --git a/rubocop/examples/bad/Style_SingleLineBlockParams.rb b/rubocop/examples/bad/Style_SingleLineBlockParams.rb new file mode 100644 index 0000000..c3c4d17 --- /dev/null +++ b/rubocop/examples/bad/Style_SingleLineBlockParams.rb @@ -0,0 +1,5 @@ +# TODO: This cop checks whether the block parameters of a single-line method +# accepting a block match the names specified via configuration. + +# TODO: For instance one can configure `reduce`(`inject`) to use |a, e| +# as parameters. diff --git a/rubocop/examples/bad/Style_SingleLineMethods.rb b/rubocop/examples/bad/Style_SingleLineMethods.rb new file mode 100644 index 0000000..de46cf5 --- /dev/null +++ b/rubocop/examples/bad/Style_SingleLineMethods.rb @@ -0,0 +1,16 @@ +# Avoid single-line methods. Although they are somewhat popular in the wild, +# there are a few peculiarities about their definition syntax +# that make their use undesirable. At any rate - there should be no +# more than one expression in a single-line method. + +# bad +def too_much; something; something_else; end + +# okish - notice that the first ; is required +def no_braces_method; body end + +# okish - notice that the second ; is optional +def no_braces_method; body; end + +# okish - valid syntax, but no ; makes it kind of hard to read +def some_method() body end diff --git a/rubocop/examples/bad/Style_SpaceAfterColon.rb b/rubocop/examples/bad/Style_SpaceAfterColon.rb new file mode 100644 index 0000000..52a25b0 --- /dev/null +++ b/rubocop/examples/bad/Style_SpaceAfterColon.rb @@ -0,0 +1,22 @@ +# Use spaces around operators, after commas, colons and semicolons, +# around { and before }. Whitespace might be (mostly) irrelevant to the +# Ruby interpreter, but its proper use is the key to +# writing easily readable code. + +sum = 1 + 2 +a, b = 1, 2 +[1, 2, 3].each { |e| puts e } +class FooError < StandardError; end +The only exception, regarding operators, is the exponent operator: + +# bad +e = M * c ** 2 + +# { and } deserve a bit of clarification, since they are used for block +# and hash literals, as well as string interpolation. +# For hash literals two styles are considered acceptable. + +# The first variant is slightly more readable (and arguably more +# popular in the Ruby community in general). The second variant has +# the advantage of adding visual difference between block and hash literals. +# Whichever one you pick - apply it consistently. diff --git a/rubocop/examples/bad/Style_SpaceAfterComma.rb b/rubocop/examples/bad/Style_SpaceAfterComma.rb new file mode 100644 index 0000000..6afc3e4 --- /dev/null +++ b/rubocop/examples/bad/Style_SpaceAfterComma.rb @@ -0,0 +1 @@ +# TODO: Checks for comma (,) not followed by some kind of space. diff --git a/rubocop/examples/bad/Style_SpaceAfterMethodName.rb b/rubocop/examples/bad/Style_SpaceAfterMethodName.rb new file mode 100644 index 0000000..1858773 --- /dev/null +++ b/rubocop/examples/bad/Style_SpaceAfterMethodName.rb @@ -0,0 +1,5 @@ +# Do not put a space between a method name and the opening parenthesis. + +# bad +f (3 + 2) + 1 + diff --git a/rubocop/examples/bad/Style_SpaceAfterNot.rb b/rubocop/examples/bad/Style_SpaceAfterNot.rb new file mode 100644 index 0000000..e2b10eb --- /dev/null +++ b/rubocop/examples/bad/Style_SpaceAfterNot.rb @@ -0,0 +1,4 @@ +# No space after !. + +# bad +! something diff --git a/rubocop/examples/bad/Style_SpaceAfterSemicolon.rb b/rubocop/examples/bad/Style_SpaceAfterSemicolon.rb new file mode 100644 index 0000000..9a72875 --- /dev/null +++ b/rubocop/examples/bad/Style_SpaceAfterSemicolon.rb @@ -0,0 +1 @@ +# TODO: Checks for semicolon (;) not followed by some kind of space. diff --git a/rubocop/examples/bad/Style_SpaceBeforeBlockBraces.rb b/rubocop/examples/bad/Style_SpaceBeforeBlockBraces.rb new file mode 100644 index 0000000..9adc002 --- /dev/null +++ b/rubocop/examples/bad/Style_SpaceBeforeBlockBraces.rb @@ -0,0 +1,2 @@ +# TODO: Checks that block braces have or don't have a space before +# the opening brace depending on configuration. diff --git a/rubocop/examples/bad/Style_SpaceBeforeComma.rb b/rubocop/examples/bad/Style_SpaceBeforeComma.rb new file mode 100644 index 0000000..2a58d0a --- /dev/null +++ b/rubocop/examples/bad/Style_SpaceBeforeComma.rb @@ -0,0 +1 @@ +# TODO: Checks for comma (,) preceded by space. diff --git a/rubocop/examples/bad/Style_SpaceBeforeComment.rb b/rubocop/examples/bad/Style_SpaceBeforeComment.rb new file mode 100644 index 0000000..5ef51ab --- /dev/null +++ b/rubocop/examples/bad/Style_SpaceBeforeComment.rb @@ -0,0 +1,4 @@ +# TODO: This cop checks for missing space between a token and a +# comment on the same line. + +# TODO: Put a space before an end-of-line comment. diff --git a/rubocop/examples/bad/Style_SpaceBeforeFirstArg.rb b/rubocop/examples/bad/Style_SpaceBeforeFirstArg.rb new file mode 100644 index 0000000..7b17eb6 --- /dev/null +++ b/rubocop/examples/bad/Style_SpaceBeforeFirstArg.rb @@ -0,0 +1,5 @@ +# TODO: Put one space between the method name and the first argument. + +# bad +something x +something y, z diff --git a/rubocop/examples/bad/Style_SpaceBeforeSemicolon.rb b/rubocop/examples/bad/Style_SpaceBeforeSemicolon.rb new file mode 100644 index 0000000..7525182 --- /dev/null +++ b/rubocop/examples/bad/Style_SpaceBeforeSemicolon.rb @@ -0,0 +1 @@ +# TODO: Checks for semicolon (;) preceded by space. diff --git a/rubocop/examples/bad/style_DoubleNegation.rb b/rubocop/examples/bad/style_DoubleNegation.rb new file mode 100644 index 0000000..2e0bc63 --- /dev/null +++ b/rubocop/examples/bad/style_DoubleNegation.rb @@ -0,0 +1,6 @@ +# This cop checks for uses of double negation (!!) +# to convert something to a boolean value. +# As this is both cryptic and usually redundant it should be avoided. + +# bad +!!something diff --git a/rubocop/examples/bad/style_EachWithObject.rb b/rubocop/examples/bad/style_EachWithObject.rb new file mode 100644 index 0000000..f37d0bb --- /dev/null +++ b/rubocop/examples/bad/style_EachWithObject.rb @@ -0,0 +1,9 @@ +# This cop looks for inject / reduce calls where the passed +# in object is returned at the end and so could be replaced +# by each_with_object without the need to return the object at the end. + +# However, we can't replace with each_with_object +# if the accumulator parameter is assigned to within the block. + +# bad +[1, 2].inject({}) { |a, e| a[e] = e; a } diff --git a/rubocop/examples/bad/style_EmptyLineBetweenDefs.rb b/rubocop/examples/bad/style_EmptyLineBetweenDefs.rb new file mode 100644 index 0000000..ae214ac --- /dev/null +++ b/rubocop/examples/bad/style_EmptyLineBetweenDefs.rb @@ -0,0 +1,14 @@ +# TODO:Use empty lines between method definitions and also +# to break up methods into logical paragraphs internally. + +# bad +def some_method + data = initialize(options) + + data.manipulate! + + data.result +end +def some_method + result +end diff --git a/rubocop/examples/bad/style_EmptyLines.rb b/rubocop/examples/bad/style_EmptyLines.rb new file mode 100644 index 0000000..0689f5c --- /dev/null +++ b/rubocop/examples/bad/style_EmptyLines.rb @@ -0,0 +1 @@ +# TODO:This cops checks for two or more consecutive blank lines diff --git a/rubocop/examples/bad/style_EmptyLinesAroundAccessModifier.rb b/rubocop/examples/bad/style_EmptyLinesAroundAccessModifier.rb new file mode 100644 index 0000000..db9939d --- /dev/null +++ b/rubocop/examples/bad/style_EmptyLinesAroundAccessModifier.rb @@ -0,0 +1,2 @@ +# TODO: Access modifiers should be surrounded by blank lines. +# TODO: Keep a blank line before and after `%s`.' diff --git a/rubocop/examples/bad/style_EmptyLinesAroundBlockBody.rb b/rubocop/examples/bad/style_EmptyLinesAroundBlockBody.rb new file mode 100644 index 0000000..7c4a6d5 --- /dev/null +++ b/rubocop/examples/bad/style_EmptyLinesAroundBlockBody.rb @@ -0,0 +1,8 @@ +# This cops checks if empty lines around +# the bodies of blocks match the configuration. + +# bad +something do + + ... +end diff --git a/rubocop/examples/bad/style_EmptyLinesAroundClassBody.rb b/rubocop/examples/bad/style_EmptyLinesAroundClassBody.rb new file mode 100644 index 0000000..77f833d --- /dev/null +++ b/rubocop/examples/bad/style_EmptyLinesAroundClassBody.rb @@ -0,0 +1,11 @@ +# TODO: Checks if empty lines around the bodies +# of classes match the configuration. + +# bad +class Test + + def something + ... + end + +end diff --git a/rubocop/examples/bad/style_EmptyLinesAroundModuleBody.rb b/rubocop/examples/bad/style_EmptyLinesAroundModuleBody.rb new file mode 100644 index 0000000..e5af619 --- /dev/null +++ b/rubocop/examples/bad/style_EmptyLinesAroundModuleBody.rb @@ -0,0 +1,10 @@ +# Checks if empty lines around the bodies of modules match the configuration. + +# bad +module Test + + def something + ... + end + +end diff --git a/rubocop/examples/bad/style_EmptyLiteral.rb b/rubocop/examples/bad/style_EmptyLiteral.rb new file mode 100644 index 0000000..3f627ba --- /dev/null +++ b/rubocop/examples/bad/style_EmptyLiteral.rb @@ -0,0 +1,2 @@ +# TODO: This cop checks for the use of a method, +# the result of which would be a literal, like an empty array, hash or string. diff --git a/rubocop/examples/bad/style_Encoding.rb b/rubocop/examples/bad/style_Encoding.rb new file mode 100644 index 0000000..e2503b4 --- /dev/null +++ b/rubocop/examples/bad/style_Encoding.rb @@ -0,0 +1,3 @@ +# TODO: This cop checks whether the source file has a utf-8 encoding comment. +# This check makes sense only in Ruby 1.9, +# since in 2.0+ utf-8 is the default source file encoding. diff --git a/rubocop/examples/bad/style_EndBlock.rb b/rubocop/examples/bad/style_EndBlock.rb new file mode 100644 index 0000000..ba30baf --- /dev/null +++ b/rubocop/examples/bad/style_EndBlock.rb @@ -0,0 +1,4 @@ +# Do not use END blocks. Use Kernel#at_exit instead. + +# bad +END { puts 'Goodbye!' } diff --git a/rubocop/examples/bad/style_EndOfLine.rb b/rubocop/examples/bad/style_EndOfLine.rb new file mode 100644 index 0000000..f4fe607 --- /dev/null +++ b/rubocop/examples/bad/style_EndOfLine.rb @@ -0,0 +1 @@ +# TODO: This cop checks for Windows-style line endings in the source code. diff --git a/rubocop/examples/bad/style_EvenOdd.rb b/rubocop/examples/bad/style_EvenOdd.rb new file mode 100644 index 0000000..2d8bab2 --- /dev/null +++ b/rubocop/examples/bad/style_EvenOdd.rb @@ -0,0 +1,5 @@ +# This cop checks for places where Fixnum#even? +# or Fixnum#odd? should have been used. + +# bad +if x % 2 == 0 diff --git a/rubocop/examples/bad/style_FileName.rb b/rubocop/examples/bad/style_FileName.rb new file mode 100644 index 0000000..5302ac9 --- /dev/null +++ b/rubocop/examples/bad/style_FileName.rb @@ -0,0 +1,3 @@ +# This cop makes sure that Ruby source files have snake_case names. + +# TODO: Use snake_case for naming files, e.g. hello_world.rb. diff --git a/rubocop/examples/bad/style_FlipFlop.rb b/rubocop/examples/bad/style_FlipFlop.rb new file mode 100644 index 0000000..251a476 --- /dev/null +++ b/rubocop/examples/bad/style_FlipFlop.rb @@ -0,0 +1 @@ +# TODO: Avoid the use of flip-flops. diff --git a/rubocop/examples/bad/style_FormatString.rb b/rubocop/examples/bad/style_FormatString.rb new file mode 100644 index 0000000..0c110e0 --- /dev/null +++ b/rubocop/examples/bad/style_FormatString.rb @@ -0,0 +1,6 @@ +# TODO: This cop enforces the use of a single string formatting utility. +# Valid options include Kernel#format, Kernel#sprintf and String#%. + +# TODO: The detection of String#% cannot be implemented in a reliable manner +# for all cases, so only two scenarios are considered - if the first argument +# is a string literal and if the second argument is an array literal. diff --git a/rubocop/examples/bad/style_GlobalVars.rb b/rubocop/examples/bad/style_GlobalVars.rb new file mode 100644 index 0000000..6e94578 --- /dev/null +++ b/rubocop/examples/bad/style_GlobalVars.rb @@ -0,0 +1,4 @@ +# Use module instance variables instead of global variables. + +# bad +$foo_bar = 1 diff --git a/rubocop/examples/bad/style_GuardClause.rb b/rubocop/examples/bad/style_GuardClause.rb new file mode 100644 index 0000000..027b1e8 --- /dev/null +++ b/rubocop/examples/bad/style_GuardClause.rb @@ -0,0 +1,12 @@ +# This cop if/unless expression that can be replace with a guard clause. +# It should be extended to handle methods whose body +# is if/else or a case expression with a default branch. + +# bad +def test + if something + work + work + work + end +end diff --git a/rubocop/examples/bad/style_HashSyntax.rb b/rubocop/examples/bad/style_HashSyntax.rb new file mode 100644 index 0000000..a89f1b9 --- /dev/null +++ b/rubocop/examples/bad/style_HashSyntax.rb @@ -0,0 +1,4 @@ +# Use the Ruby 1.9 hash literal syntax when your hash keys are symbols. + +# bad +hash = { :one => 1, :two => 2, :three => 3 } diff --git a/rubocop/examples/bad/style_IfUnlessModifier.rb b/rubocop/examples/bad/style_IfUnlessModifier.rb new file mode 100644 index 0000000..177363d --- /dev/null +++ b/rubocop/examples/bad/style_IfUnlessModifier.rb @@ -0,0 +1,7 @@ +# Favor modifier if/unless usage when you have a single-line body. +# Another good alternative is the usage of control flow &&/||. + +# bad +if some_condition + do_something +end diff --git a/rubocop/examples/bad/style_IfWithSemicolon.rb b/rubocop/examples/bad/style_IfWithSemicolon.rb new file mode 100644 index 0000000..2d652f6 --- /dev/null +++ b/rubocop/examples/bad/style_IfWithSemicolon.rb @@ -0,0 +1,4 @@ +# Do not use if x; .... Use the ternary operator instead. + +# bad +result = if some_condition; something else something_else end diff --git a/rubocop/examples/bad/style_IndentArray.rb b/rubocop/examples/bad/style_IndentArray.rb new file mode 100644 index 0000000..14b35f6 --- /dev/null +++ b/rubocop/examples/bad/style_IndentArray.rb @@ -0,0 +1,6 @@ +# TODO: This cops checks the indentation of the first element in an array +# literal where the opening bracket and the first element are on separate +# lines. The other elements' indentations are handled by the AlignArray cop. + +# TODO: Array literals shall have their first element indented one step +# (2 spaces) more than the start of the line where the opening bracket is. diff --git a/rubocop/examples/bad/style_IndentHash.rb b/rubocop/examples/bad/style_IndentHash.rb new file mode 100644 index 0000000..1b00a59 --- /dev/null +++ b/rubocop/examples/bad/style_IndentHash.rb @@ -0,0 +1,11 @@ +# TODO: This cops checks the indentation of the first key in a hash literal +# where the opening brace and the first key are on separate lines. The other keys' +# indentations are handled by the AlignHash cop + +# TODO: Hash literals that are arguments in a method call with parentheses, +# and where the opening curly brace of the hash is on the same line as the +# opening parenthesis of the method call, shall have their first key indented +# one step (two spaces) more than the position inside the opening parenthesis. + +# TODO: Other hash literals shall have their first key indented one step more +# than the start of the line where the opening curly brace is. diff --git a/rubocop/examples/bad/style_IndentationConsistency.rb b/rubocop/examples/bad/style_IndentationConsistency.rb new file mode 100644 index 0000000..022c74e --- /dev/null +++ b/rubocop/examples/bad/style_IndentationConsistency.rb @@ -0,0 +1,7 @@ +# This cops checks for inconsistent indentation. + +# bad +def test + puts 'hello' + puts 'world' +end diff --git a/rubocop/examples/bad/style_IndentationWidth.rb b/rubocop/examples/bad/style_IndentationWidth.rb new file mode 100644 index 0000000..3d46de0 --- /dev/null +++ b/rubocop/examples/bad/style_IndentationWidth.rb @@ -0,0 +1,6 @@ +# Use two spaces per indentation level (aka soft tabs). No hard tabs. + +# bad - four spaces +def some_method + do_something +end diff --git a/rubocop/examples/bad/style_Lambda.rb b/rubocop/examples/bad/style_Lambda.rb new file mode 100644 index 0000000..4f79145 --- /dev/null +++ b/rubocop/examples/bad/style_Lambda.rb @@ -0,0 +1,6 @@ +# TODO: This cop checks for uses of the pre 1.9 lambda syntax for one-line +# anonymous functions and uses of the 1.9 lambda syntax for multi-line +# anonymous functions. + +# TODO: 'Use the new lambda literal syntax `->(params) {...}`.' +# TODO: 'Use the `lambda` method for multi-line lambdas.' diff --git a/rubocop/examples/bad/style_LambdaCall.rb b/rubocop/examples/bad/style_LambdaCall.rb new file mode 100644 index 0000000..f08910d --- /dev/null +++ b/rubocop/examples/bad/style_LambdaCall.rb @@ -0,0 +1,9 @@ +# Prefer proc.call() over proc[] or proc.() for both lambdas and procs. + +# bad - looks similar to Enumeration access +l = ->(v) { puts v } +l[1] + +# also bad - uncommon syntax +l = ->(v) { puts v } +l.(1) diff --git a/rubocop/examples/bad/style_LeadingCommentSpace.rb b/rubocop/examples/bad/style_LeadingCommentSpace.rb new file mode 100644 index 0000000..4342ed6 --- /dev/null +++ b/rubocop/examples/bad/style_LeadingCommentSpace.rb @@ -0,0 +1,4 @@ +# TODO: his cop checks whether comments have a leading space +# after the # denoting the start of the comment. +# The leading space is not required for some RDoc special syntax, +# like #++, #–, #:nodoc, etc. diff --git a/rubocop/examples/bad/style_LineEndConcatenation.rb b/rubocop/examples/bad/style_LineEndConcatenation.rb new file mode 100644 index 0000000..57c331d --- /dev/null +++ b/rubocop/examples/bad/style_LineEndConcatenation.rb @@ -0,0 +1,9 @@ +# This cop checks for string literal concatenation at the end of a line. + + +# bad +some_str = 'ala' + + 'bala' + +some_str = 'ala' << + 'bala' diff --git a/rubocop/examples/bad/style_LineLength.rb b/rubocop/examples/bad/style_LineLength.rb new file mode 100644 index 0000000..31b51fd --- /dev/null +++ b/rubocop/examples/bad/style_LineLength.rb @@ -0,0 +1 @@ +# TODO: Limit lines to 80 characters. diff --git a/rubocop/examples/bad/style_MethodCallParentheses.rb b/rubocop/examples/bad/style_MethodCallParentheses.rb new file mode 100644 index 0000000..f243972 --- /dev/null +++ b/rubocop/examples/bad/style_MethodCallParentheses.rb @@ -0,0 +1,7 @@ +# Omit parentheses for method calls with no arguments. + +# bad +Kernel.exit!() +2.even?() +fork() +'test'.upcase() diff --git a/rubocop/examples/bad/style_MethodDefParentheses.rb b/rubocop/examples/bad/style_MethodDefParentheses.rb new file mode 100644 index 0000000..996ded0 --- /dev/null +++ b/rubocop/examples/bad/style_MethodDefParentheses.rb @@ -0,0 +1,12 @@ +# Use def with parentheses when there are parameters. Omit the parentheses when +# the method doesn't accept any parameters. + +# bad +def some_method() + # body omitted +end + +# bad +def some_method_with_parameters param1, param2 + # body omitted +end diff --git a/rubocop/examples/bad/style_MethodName.rb b/rubocop/examples/bad/style_MethodName.rb new file mode 100644 index 0000000..5db3b0b --- /dev/null +++ b/rubocop/examples/bad/style_MethodName.rb @@ -0,0 +1,16 @@ +# Use snake_case for symbols, methods and variables. + +# bad +:'some symbol' +:SomeSymbol +:someSymbol + +someVar = 5 + +def someMethod + ... +end + +def SomeMethod +... +end diff --git a/rubocop/examples/bad/style_ModuleFunction.rb b/rubocop/examples/bad/style_ModuleFunction.rb new file mode 100644 index 0000000..064e3ac --- /dev/null +++ b/rubocop/examples/bad/style_ModuleFunction.rb @@ -0,0 +1,15 @@ +# Favor the use of module_function over extend self when you want +# to turn a module's instance methods into class methods. + +# bad +module Utilities + extend self + + def parse_something(string) + # do stuff here + end + + def other_utility_method(number, string) + # do some more stuff + end +end diff --git a/rubocop/examples/bad/style_MultilineBlockChain.rb b/rubocop/examples/bad/style_MultilineBlockChain.rb new file mode 100644 index 0000000..3e828d0 --- /dev/null +++ b/rubocop/examples/bad/style_MultilineBlockChain.rb @@ -0,0 +1,16 @@ +# Prefer {...} over do...end for single-line blocks. +# Avoid using {...} for multi-line blocks (multiline chaining is always ugly). +# Always use do...end for "control flow" and "method definitions" +# (e.g. in Rakefiles and certain DSLs). Avoid do...end when chaining. + +names = %w(Bozhidar Steve Sarah) + +# bad +names.each do |name| + puts name +end + +# bad +names.select do |name| + name.start_with?('S') +end.map { |name| name.upcase } diff --git a/rubocop/examples/bad/style_MultilineIfThen.rb b/rubocop/examples/bad/style_MultilineIfThen.rb new file mode 100644 index 0000000..5d54e17 --- /dev/null +++ b/rubocop/examples/bad/style_MultilineIfThen.rb @@ -0,0 +1,6 @@ +# Do not use then for multi-line if/unless. + +# bad +if some_condition then + # body omitted +end diff --git a/rubocop/examples/bad/style_MultilineTernaryOperator.rb b/rubocop/examples/bad/style_MultilineTernaryOperator.rb new file mode 100644 index 0000000..35ff17c --- /dev/null +++ b/rubocop/examples/bad/style_MultilineTernaryOperator.rb @@ -0,0 +1 @@ +# TODO: Avoid multi-line ?: (the ternary operator); use if/unless instead. diff --git a/rubocop/examples/bad/style_NegatedIf.rb b/rubocop/examples/bad/style_NegatedIf.rb new file mode 100644 index 0000000..72b5324 --- /dev/null +++ b/rubocop/examples/bad/style_NegatedIf.rb @@ -0,0 +1,7 @@ +# Favor unless over if for negative conditions (or control flow ||). + +# bad +do_something if !some_condition + +# bad +do_something if not some_condition diff --git a/rubocop/examples/bad/style_NegatedWhile.rb b/rubocop/examples/bad/style_NegatedWhile.rb new file mode 100644 index 0000000..eba2daa --- /dev/null +++ b/rubocop/examples/bad/style_NegatedWhile.rb @@ -0,0 +1,4 @@ +# Favor until over while for negative conditions. + +# bad +do_something while !some_condition diff --git a/rubocop/examples/bad/style_NestedTernaryOperator.rb b/rubocop/examples/bad/style_NestedTernaryOperator.rb new file mode 100644 index 0000000..c0136c0 --- /dev/null +++ b/rubocop/examples/bad/style_NestedTernaryOperator.rb @@ -0,0 +1,6 @@ +# Use one expression per branch in a ternary operator. +# This also means that ternary operators must not be nested. +# Prefer if/else constructs in these cases. [link] + +# bad +some_condition ? (nested_condition ? nested_something : nested_something_else) : something_else diff --git a/rubocop/examples/bad/style_Next.rb b/rubocop/examples/bad/style_Next.rb new file mode 100644 index 0000000..bc74038 --- /dev/null +++ b/rubocop/examples/bad/style_Next.rb @@ -0,0 +1,8 @@ +# Use `next` to skip iteration instead of a condition at the end. + +# bad +[1, 2].each do |a| + if a == 1 do + puts a + end +end diff --git a/rubocop/examples/bad/style_NilComparison.rb b/rubocop/examples/bad/style_NilComparison.rb new file mode 100644 index 0000000..08dc3e6 --- /dev/null +++ b/rubocop/examples/bad/style_NilComparison.rb @@ -0,0 +1,12 @@ +# Favor the use of predicate methods to explicit comparisons +# with ==. Numeric comparisons are OK. + +# bad +if x % 2 == 0 +end + +if x % 2 == 1 +end + +if x == nil +end diff --git a/rubocop/examples/bad/style_NonNilCheck.rb b/rubocop/examples/bad/style_NonNilCheck.rb new file mode 100644 index 0000000..f769f38 --- /dev/null +++ b/rubocop/examples/bad/style_NonNilCheck.rb @@ -0,0 +1,5 @@ +# Don't do explicit non-nil checks unless you're dealing with boolean values. + +# bad +do_something if !something.nil? +do_something if something != nil diff --git a/rubocop/examples/bad/style_Not.rb b/rubocop/examples/bad/style_Not.rb new file mode 100644 index 0000000..da37b79 --- /dev/null +++ b/rubocop/examples/bad/style_Not.rb @@ -0,0 +1,8 @@ +# Don't use block comments. They cannot be preceded by whitespace and +# are not as easy to spot as regular comments. [link] + +# bad +=begin +comment line +another comment line +=end diff --git a/rubocop/examples/bad/style_NumericLiterals.rb b/rubocop/examples/bad/style_NumericLiterals.rb new file mode 100644 index 0000000..ce3cba9 --- /dev/null +++ b/rubocop/examples/bad/style_NumericLiterals.rb @@ -0,0 +1,2 @@ +# TODO: his cop checks for big numeric literals +# without _ between groups of digits in them. diff --git a/rubocop/examples/bad/style_OneLineConditional.rb b/rubocop/examples/bad/style_OneLineConditional.rb new file mode 100644 index 0000000..0f66442 --- /dev/null +++ b/rubocop/examples/bad/style_OneLineConditional.rb @@ -0,0 +1,5 @@ +# Favor the ternary operator(?:) over if/then/else/end constructs. +# It's more common and obviously more concise. [link] + +# bad +result = if some_condition then something else something_else end diff --git a/rubocop/examples/bad/style_OpMethod.rb b/rubocop/examples/bad/style_OpMethod.rb new file mode 100644 index 0000000..c8fcaa0 --- /dev/null +++ b/rubocop/examples/bad/style_OpMethod.rb @@ -0,0 +1,6 @@ +# When defining binary operators, name the parameter other(<< and [] are +# exceptions to the rule, since their semantics are different). [link] + +def +(other) + # body omitted +end diff --git a/rubocop/examples/bad/style_ParenthesesAroundCondition.rb b/rubocop/examples/bad/style_ParenthesesAroundCondition.rb new file mode 100644 index 0000000..282e40a --- /dev/null +++ b/rubocop/examples/bad/style_ParenthesesAroundCondition.rb @@ -0,0 +1,6 @@ +# Don't use parentheses around the condition of an if/unless/while/until. + +# bad +if (x > 10) + # body omitted +end diff --git a/rubocop/examples/bad/style_PercentLiteralDelimiters.rb b/rubocop/examples/bad/style_PercentLiteralDelimiters.rb new file mode 100644 index 0000000..ef783fd --- /dev/null +++ b/rubocop/examples/bad/style_PercentLiteralDelimiters.rb @@ -0,0 +1 @@ +# TODO: This cop enforces the consitent useage of `%`-literal delimiters. diff --git a/rubocop/examples/bad/style_PerlBackrefs.rb b/rubocop/examples/bad/style_PerlBackrefs.rb new file mode 100644 index 0000000..9b3cb87 --- /dev/null +++ b/rubocop/examples/bad/style_PerlBackrefs.rb @@ -0,0 +1,8 @@ +# Don't use the cryptic Perl-legacy variables denoting last regexp group matches +# ($1, $2, etc). Use Regexp.last_match(n) instead. + +/(regexp)/ =~ string +... + +# bad +process $1 diff --git a/rubocop/examples/bad/style_PredicateName.rb b/rubocop/examples/bad/style_PredicateName.rb new file mode 100644 index 0000000..a46e7d0 --- /dev/null +++ b/rubocop/examples/bad/style_PredicateName.rb @@ -0,0 +1,3 @@ +# TODO: The names of predicate methods (methods that return a boolean value) +# should end in a question mark. (i.e. Array#empty?). Methods that don't +# return a boolean, shouldn't end in a question mark diff --git a/rubocop/examples/bad/style_Proc.rb b/rubocop/examples/bad/style_Proc.rb new file mode 100644 index 0000000..4037359 --- /dev/null +++ b/rubocop/examples/bad/style_Proc.rb @@ -0,0 +1,4 @@ +# Prefer proc over Proc.new. + +# bad +p = Proc.new { |n| puts n } diff --git a/rubocop/examples/bad/style_RaiseArgs.rb b/rubocop/examples/bad/style_RaiseArgs.rb new file mode 100644 index 0000000..1e1e1d9 --- /dev/null +++ b/rubocop/examples/bad/style_RaiseArgs.rb @@ -0,0 +1 @@ +# TODO: This cop checks the args passed to `fail` and `raise`. diff --git a/rubocop/examples/bad/style_RedundantBegin.rb b/rubocop/examples/bad/style_RedundantBegin.rb new file mode 100644 index 0000000..98efe42 --- /dev/null +++ b/rubocop/examples/bad/style_RedundantBegin.rb @@ -0,0 +1 @@ +# TODO: This cop checks for redundant begin blocks diff --git a/rubocop/examples/bad/style_RedundantException.rb b/rubocop/examples/bad/style_RedundantException.rb new file mode 100644 index 0000000..90cd154 --- /dev/null +++ b/rubocop/examples/bad/style_RedundantException.rb @@ -0,0 +1,4 @@ +# Don't specify RuntimeError explicitly in the two argument version of raise. + +# bad +raise RuntimeError, 'message' diff --git a/rubocop/examples/bad/style_RedundantReturn.rb b/rubocop/examples/bad/style_RedundantReturn.rb new file mode 100644 index 0000000..eccc5d9 --- /dev/null +++ b/rubocop/examples/bad/style_RedundantReturn.rb @@ -0,0 +1,6 @@ +# Avoid return where not required for flow of control. + +# bad +def some_method(some_arr) + return some_arr.size +end diff --git a/rubocop/examples/bad/style_RedundantSelf.rb b/rubocop/examples/bad/style_RedundantSelf.rb new file mode 100644 index 0000000..e643e9f --- /dev/null +++ b/rubocop/examples/bad/style_RedundantSelf.rb @@ -0,0 +1,7 @@ +# TODO: This cop checks for redundant uses of `self`. + +# `self` is only needed when: +# Sending a message to same object with zero arguments in presence of a method +# name clash with an argument or a local variable. +# Note, with using explicit self you can only send messages with public +# or protected scope, you cannot send private messages this way. diff --git a/rubocop/examples/bad/style_RegexpLiteral.rb b/rubocop/examples/bad/style_RegexpLiteral.rb new file mode 100644 index 0000000..112176a --- /dev/null +++ b/rubocop/examples/bad/style_RegexpLiteral.rb @@ -0,0 +1,4 @@ +# Use %r only for regular expressions matching at least one '/' character. + +# bad +%r{\s+} diff --git a/rubocop/examples/bad/style_RescueModifier.rb b/rubocop/examples/bad/style_RescueModifier.rb new file mode 100644 index 0000000..04cfb38 --- /dev/null +++ b/rubocop/examples/bad/style_RescueModifier.rb @@ -0,0 +1 @@ +# Avoid using `rescue` in its modifier form. diff --git a/rubocop/examples/bad/style_for.rb b/rubocop/examples/bad/style_for.rb new file mode 100644 index 0000000..80bfcfb --- /dev/null +++ b/rubocop/examples/bad/style_for.rb @@ -0,0 +1,16 @@ +# Do not use for, unless you know exactly why. +# Most of the time iterators should be used instead. +# for is implemented in terms of each +# (so you're adding a level of indirection), +# but with a twist - for doesn't introduce a new scope (unlike each) +# and variables defined in its block will be visible outside it. + +arr = [1, 2, 3] + +# bad +for elem in arr do + puts elem +end + +# note that elem is accessible outside of the for loop +elem # => 3 diff --git a/rubocop/examples/good/Style_AccessModifierIndentation.rb b/rubocop/examples/good/Style_AccessModifierIndentation.rb new file mode 100644 index 0000000..79ca990 --- /dev/null +++ b/rubocop/examples/good/Style_AccessModifierIndentation.rb @@ -0,0 +1,21 @@ +# 'https://github.com/bbatsov/ruby-style-guide#indent-public-private-protected' + +# Indent the public, protected, and private methods as much as the method definitions they apply to. +# Leave one blank line above the visibility modifier and one blank line below in order to emphasize that it applies to all methods below it + +# good +class SomeClass + def public_method + # ... + end + + private + + def private_method + # ... + end + + def another_private_method + # ... + end +end diff --git a/rubocop/examples/good/Style_Alias.rb b/rubocop/examples/good/Style_Alias.rb new file mode 100644 index 0000000..1556d4f --- /dev/null +++ b/rubocop/examples/good/Style_Alias.rb @@ -0,0 +1,16 @@ +# 'https://github.com/bbatsov/ruby-style-guide#alias-method' + +# Always use alias_method when aliasing methods of modules, classes, or singleton classes at runtime +# as the lexical scope of alias leads to unpredictability in these cases. + +# good + +module Mononymous + def self.included(other) + other.class_eval { alias_method :full_name, :given_name } + end +end + +class Sting < Westerner + include Mononymous +end diff --git a/rubocop/examples/good/Style_AlignHash.rb b/rubocop/examples/good/Style_AlignHash.rb new file mode 100644 index 0000000..3b367ca --- /dev/null +++ b/rubocop/examples/good/Style_AlignHash.rb @@ -0,0 +1,2 @@ +# TODO: Here we check if the keys, separators, +# and values of a multi-line hash literal are aligned. diff --git a/rubocop/examples/good/Style_AlignParameters.rb b/rubocop/examples/good/Style_AlignParameters.rb new file mode 100644 index 0000000..10c1f72 --- /dev/null +++ b/rubocop/examples/good/Style_AlignParameters.rb @@ -0,0 +1,28 @@ +# https://github.com/bbatsov/ruby-style-guide#no-double-indent + +# Align the parameters of a method call if they span more than one line. +# When aligning parameters is not appropriate due to line-length constraints, +# single indent for the lines after the first is also acceptable. + +# starting point (line is too long) +def send_mail(source) + Mailer.deliver(to: 'bob@example.com', from: 'us@example.com', subject: 'Important message', body: source.text) +end + +# good +def send_mail(source) + Mailer.deliver(to: 'bob@example.com', + from: 'us@example.com', + subject: 'Important message', + body: source.text) +end + +# good (normal indent) +def send_mail(source) + Mailer.deliver( + to: 'bob@example.com', + from: 'us@example.com', + subject: 'Important message', + body: source.text + ) +end diff --git a/rubocop/examples/good/Style_AndOr.rb b/rubocop/examples/good/Style_AndOr.rb new file mode 100644 index 0000000..3ff916a --- /dev/null +++ b/rubocop/examples/good/Style_AndOr.rb @@ -0,0 +1,13 @@ +# 'https://github.com/bbatsov/ruby-style-guide#no-and-or-or' + +# The and and or keywords are banned. +# It's just not worth it. Always use && and || instead. + +# good +# boolean expression +if some_condition && some_other_condition + do_something +end + +# control flow +document.saved? || document.save! diff --git a/rubocop/examples/good/Style_ArrayJoin.rb b/rubocop/examples/good/Style_ArrayJoin.rb new file mode 100644 index 0000000..26f99ab --- /dev/null +++ b/rubocop/examples/good/Style_ArrayJoin.rb @@ -0,0 +1,7 @@ +# 'https://github.com/bbatsov/ruby-style-guide#array-join' + +# Favor the use of Array#join over the fairly cryptic Array#* with a string argument. + +# good +%w(one two three).join(', ') +# => 'one, two, three' diff --git a/rubocop/examples/good/Style_AsciiIdentifiers.rb b/rubocop/examples/good/Style_AsciiIdentifiers.rb new file mode 100644 index 0000000..de74c24 --- /dev/null +++ b/rubocop/examples/good/Style_AsciiIdentifiers.rb @@ -0,0 +1,6 @@ +# 'https://github.com/bbatsov/ruby-style-guide#english-identifiers' + +# Name identifiers in English. + +# good +salary = 1_000 diff --git a/rubocop/examples/good/Style_Attr.rb b/rubocop/examples/good/Style_Attr.rb new file mode 100644 index 0000000..4faff75 --- /dev/null +++ b/rubocop/examples/good/Style_Attr.rb @@ -0,0 +1,7 @@ +# 'https://github.com/bbatsov/ruby-style-guide#attr' + +# Avoid the use of attr. Use attr_reader and attr_accessor instead. + +# good +attr_accessor :something +attr_reader :one, :two, :three diff --git a/rubocop/examples/good/Style_BeginBlock.rb b/rubocop/examples/good/Style_BeginBlock.rb new file mode 100644 index 0000000..d3a00b5 --- /dev/null +++ b/rubocop/examples/good/Style_BeginBlock.rb @@ -0,0 +1,3 @@ +# 'https://github.com/bbatsov/ruby-style-guide#no-BEGIN-blocks' + +#TODO: Avoid the use of BEGIN blocks. diff --git a/rubocop/examples/good/Style_BlockComments.rb b/rubocop/examples/good/Style_BlockComments.rb new file mode 100644 index 0000000..0b48147 --- /dev/null +++ b/rubocop/examples/good/Style_BlockComments.rb @@ -0,0 +1,8 @@ +# 'https://github.com/bbatsov/ruby-style-guide#no-block-comments' + +# Don't use block comments. +# They cannot be preceded by whitespace and are not as easy to spot as regular comments + +# good +# comment line +# another comment line diff --git a/rubocop/examples/good/Style_BlockDelimiters.rb b/rubocop/examples/good/Style_BlockDelimiters.rb new file mode 100644 index 0000000..f37c1de --- /dev/null +++ b/rubocop/examples/good/Style_BlockDelimiters.rb @@ -0,0 +1,14 @@ +# 'https://github.com/bbatsov/ruby-style-guide#single-line-blocks' + +# Prefer {...} over do...end for single-line blocks. +# Avoid using {...} for multi-line blocks (multiline chaining is always ugly). +# Always use do...end for "control flow" and "method definitions" (e.g. +# in Rakefiles and certain DSLs). Avoid do...end when chaining. + +names = %w(Bozhidar Steve Sarah) + +# good +names.each { |name| puts name } + +# good +names.select { |name| name.start_with?('S') }.map(&:upcase) diff --git a/rubocop/examples/good/Style_BlockNesting.rb b/rubocop/examples/good/Style_BlockNesting.rb new file mode 100644 index 0000000..2e364c9 --- /dev/null +++ b/rubocop/examples/good/Style_BlockNesting.rb @@ -0,0 +1,5 @@ +# TODO:This cop checks for excessive nesting of conditional and looping constructs. +# Despite the cop's name, +# blocks are not considered as an extra level of nesting. + +# The maximum level of nesting allowed is configurable. diff --git a/rubocop/examples/good/Style_BracesAroundHashParameters.rb b/rubocop/examples/good/Style_BracesAroundHashParameters.rb new file mode 100644 index 0000000..9aabc91 --- /dev/null +++ b/rubocop/examples/good/Style_BracesAroundHashParameters.rb @@ -0,0 +1,2 @@ +# TODO: This cop checks for braces around the last parameter +# in a method call if the last parameter is a hash. diff --git a/rubocop/examples/good/Style_CaseEquality.rb b/rubocop/examples/good/Style_CaseEquality.rb new file mode 100644 index 0000000..237e675 --- /dev/null +++ b/rubocop/examples/good/Style_CaseEquality.rb @@ -0,0 +1,10 @@ +# 'https://github.com/bbatsov/ruby-style-guide#no-case-equality' + +# Avoid explicit use of the case equality operator ===. +# As its name implies it is meant to be used implicitly by case expressions +# and outside of them it yields some pretty confusing code. + +# good +something.is_a?(Array) +(1..100).include?(7) +some_string =~ /something/ diff --git a/rubocop/examples/good/Style_CaseIndentation.rb b/rubocop/examples/good/Style_CaseIndentation.rb new file mode 100644 index 0000000..a09d4a8 --- /dev/null +++ b/rubocop/examples/good/Style_CaseIndentation.rb @@ -0,0 +1,17 @@ +# 'https://github.com/bbatsov/ruby-style-guide#indent-when-to-case' + +# Indent when as deep as case. +# This is the style established in both "The Ruby Programming Language" +# and "Programming Ruby". + +# good +case +when song.name == 'Misty' + puts 'Not again!' +when song.duration > 120 + puts 'Too long!' +when Time.now.hour > 21 + puts "It's too late" +else + song.play +end diff --git a/rubocop/examples/good/Style_CharacterLiteral.rb b/rubocop/examples/good/Style_CharacterLiteral.rb new file mode 100644 index 0000000..65d4175 --- /dev/null +++ b/rubocop/examples/good/Style_CharacterLiteral.rb @@ -0,0 +1,8 @@ +# 'https://github.com/bbatsov/ruby-style-guide#no-character-literals' + +# Don't use the character literal syntax ?x. +# Since Ruby 1.9 it's basically redundant - ?x would interpreted as 'x' +# (a string with a single character in it). + +# good +char = 'c' diff --git a/rubocop/examples/good/Style_ClassAndModuleCamelCase.rb b/rubocop/examples/good/Style_ClassAndModuleCamelCase.rb new file mode 100644 index 0000000..b497209 --- /dev/null +++ b/rubocop/examples/good/Style_ClassAndModuleCamelCase.rb @@ -0,0 +1,16 @@ +# 'https://github.com/bbatsov/ruby-style-guide#camelcase-classes' + +# Use CamelCase for classes and modules. + +# good +class SomeClass + ... +end + +class SomeXML + ... +end + +class XMLSomething + ... +end diff --git a/rubocop/examples/good/Style_ClassAndModuleChildren.rb b/rubocop/examples/good/Style_ClassAndModuleChildren.rb new file mode 100644 index 0000000..9b80932 --- /dev/null +++ b/rubocop/examples/good/Style_ClassAndModuleChildren.rb @@ -0,0 +1,7 @@ +# TODO:This cop checks the style of children definitions at classes and modules. +# Basically there are two different styles: +# nested - have each child on its own line + +# good +class Foo::Bar +end diff --git a/rubocop/examples/good/Style_ClassCheck.rb b/rubocop/examples/good/Style_ClassCheck.rb new file mode 100644 index 0000000..eb70060 --- /dev/null +++ b/rubocop/examples/good/Style_ClassCheck.rb @@ -0,0 +1 @@ +# TODO: This cop enforces consistent use of `Object#is_a?` or `Object#kind_of?`. diff --git a/rubocop/examples/good/Style_ClassMethods.rb b/rubocop/examples/good/Style_ClassMethods.rb new file mode 100644 index 0000000..88bc0d7 --- /dev/null +++ b/rubocop/examples/good/Style_ClassMethods.rb @@ -0,0 +1,23 @@ +StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#def-self-class-methods' + +# Use def self.method to define class methods. +# This makes the code easier to refactor since the class name is not repeated. + +class TestClass + # good + def self.some_other_method + # body omitted + end + + # Also possible and convenient when you + # have to define many class methods. + class << self + def first_method + # body omitted + end + + def second_method_etc + # body omitted + end + end +end diff --git a/rubocop/examples/good/Style_ClassVars.rb b/rubocop/examples/good/Style_ClassVars.rb new file mode 100644 index 0000000..245a48b --- /dev/null +++ b/rubocop/examples/good/Style_ClassVars.rb @@ -0,0 +1,22 @@ +# 'https://github.com/bbatsov/ruby-style-guide#no-class-vars' + +# Avoid the usage of class (@@) variables due to their "nasty" +# behavior in inheritance. + +class Parent + @@class_var = 'parent' + + def self.print_class_var + puts @@class_var + end +end + +class Child < Parent + @@class_var = 'child' +end + +Parent.print_class_var # => will print 'child' + +# As you can see all the classes in a class hierarchy actually +# share one class variable. +# Class instance variables should usually be preferred over class variables. diff --git a/rubocop/examples/good/Style_CollectionMethods.rb b/rubocop/examples/good/Style_CollectionMethods.rb new file mode 100644 index 0000000..1de2f98 --- /dev/null +++ b/rubocop/examples/good/Style_CollectionMethods.rb @@ -0,0 +1,5 @@ +# TODO: This cop checks for uses of unidiomatic method +# names from the Enumerable module. + +# TODO: The current definition of the check is flawed and should be enhanced +# by check for by blocks & procs as arguments of the methods. diff --git a/rubocop/examples/good/Style_ColonMethodCall.rb b/rubocop/examples/good/Style_ColonMethodCall.rb new file mode 100644 index 0000000..0f73c3a --- /dev/null +++ b/rubocop/examples/good/Style_ColonMethodCall.rb @@ -0,0 +1,11 @@ +# 'https://github.com/bbatsov/ruby-style-guide#double-colons' + +# Use :: only to reference constants(this includes classes and modules) and +# constructors (like Array() or Nokogiri::HTML()). +# Do not use :: for regular method invocation. + +# good +SomeClass.some_method +some_object.some_method +SomeModule::SomeClass::SOME_CONST +SomeModule::SomeClass() diff --git a/rubocop/examples/good/Style_CommentAnnotation.rb b/rubocop/examples/good/Style_CommentAnnotation.rb new file mode 100644 index 0000000..892cf58 --- /dev/null +++ b/rubocop/examples/good/Style_CommentAnnotation.rb @@ -0,0 +1,4 @@ +# 'https://github.com/bbatsov/ruby-style-guide#annotate-keywords' + +# TODO:The annotation keyword is followed by a colon and a space, +# then a note describing the problem. diff --git a/rubocop/examples/good/Style_CommentIndentation.rb b/rubocop/examples/good/Style_CommentIndentation.rb new file mode 100644 index 0000000..62d8329 --- /dev/null +++ b/rubocop/examples/good/Style_CommentIndentation.rb @@ -0,0 +1 @@ +# TODO: This cops checks the indentation of comments. diff --git a/rubocop/examples/good/Style_ConstantName.rb b/rubocop/examples/good/Style_ConstantName.rb new file mode 100644 index 0000000..9161a5d --- /dev/null +++ b/rubocop/examples/good/Style_ConstantName.rb @@ -0,0 +1,6 @@ +# 'https://github.com/bbatsov/ruby-style-guide#screaming-snake-case' + +# Use SCREAMING_SNAKE_CASE for other constants. + +# good +SOME_CONST = 5 diff --git a/rubocop/examples/good/Style_DefWithParentheses.rb b/rubocop/examples/good/Style_DefWithParentheses.rb new file mode 100644 index 0000000..a70f475 --- /dev/null +++ b/rubocop/examples/good/Style_DefWithParentheses.rb @@ -0,0 +1,14 @@ +# 'https://github.com/bbatsov/ruby-style-guide#method-parens' + +# Use def with parentheses when there are parameters. +# Omit the parentheses when the method doesn't accept any parameters. + +# good +def some_method + # body omitted +end + +# good +def some_method_with_parameters(param1, param2) + # body omitted +end diff --git a/rubocop/examples/good/Style_DeprecatedHashMethods.rb b/rubocop/examples/good/Style_DeprecatedHashMethods.rb new file mode 100644 index 0000000..373bce9 --- /dev/null +++ b/rubocop/examples/good/Style_DeprecatedHashMethods.rb @@ -0,0 +1,9 @@ +# 'https://github.com/bbatsov/ruby-style-guide#hash-key' + +# Use Hash#key? instead of Hash#has_key? and Hash#value? +# instead of Hash#has_value?. As noted here by Matz, +# the longer forms are considered deprecated. + +# good +hash.key?(:test) +hash.value?(value) diff --git a/rubocop/examples/good/Style_Documentation.rb b/rubocop/examples/good/Style_Documentation.rb new file mode 100644 index 0000000..db3c533 --- /dev/null +++ b/rubocop/examples/good/Style_Documentation.rb @@ -0,0 +1,4 @@ +# TODO: This cop checks for missing top-level documentation +# of classes and modules. Classes with no body are exempt from the check and +# so are namespace modules - modules that have nothing +# in their bodies except classes or other other modules. diff --git a/rubocop/examples/good/Style_DotPosition.rb b/rubocop/examples/good/Style_DotPosition.rb new file mode 100644 index 0000000..d4970c5 --- /dev/null +++ b/rubocop/examples/good/Style_DotPosition.rb @@ -0,0 +1,22 @@ +# 'https://github.com/bbatsov/ruby-style-guide#consistent-multi-line-chains' + +# Adopt a consistent multi-line method chaining style. +# There are two popular styles in the Ruby community, +# both of which are considered good - leading . (Option A) and +# trailing . (Option B). + +# (Option A) When continuing a chained method invocation +# on another line keep the . on the second line. + +# good - it's immediately clear what's going on the second line +one.two.three + .four + +# (Option B) When continuing a chained method invocation on another line, +# include the . on the first line to indicate that the expression continues. + +# good - it's immediately clear that the expression +# continues beyond the first line +one.two.three. + four + diff --git a/rubocop/examples/good/Style_SelfAssignment.rb b/rubocop/examples/good/Style_SelfAssignment.rb new file mode 100644 index 0000000..163d065 --- /dev/null +++ b/rubocop/examples/good/Style_SelfAssignment.rb @@ -0,0 +1,9 @@ +# Use shorthand self assignment operators whenever applicable. + +# good +x += y +x *= y +x **= y +x /= y +x ||= y +x &&= y diff --git a/rubocop/examples/good/Style_Semicolon.rb b/rubocop/examples/good/Style_Semicolon.rb new file mode 100644 index 0000000..af9db2e --- /dev/null +++ b/rubocop/examples/good/Style_Semicolon.rb @@ -0,0 +1,10 @@ +# Don't use ; to separate statements and expressions. +# As a corollary - use one expression per line. + +# good +puts 'foobar' + +puts 'foo' +puts 'bar' + +puts 'foo', 'bar' # this applies to puts in particular diff --git a/rubocop/examples/good/Style_SignalException.rb b/rubocop/examples/good/Style_SignalException.rb new file mode 100644 index 0000000..65fdce6 --- /dev/null +++ b/rubocop/examples/good/Style_SignalException.rb @@ -0,0 +1,4 @@ +# TODO: This cop checks for uses of `fail` and `raise`. + +# TODO: 'Use `fail` instead of `raise` to signal exceptions.' +# TODO: 'Use `raise` instead of `fail` to rethrow exceptions.' diff --git a/rubocop/examples/good/Style_SingleLineBlockParams.rb b/rubocop/examples/good/Style_SingleLineBlockParams.rb new file mode 100644 index 0000000..c3c4d17 --- /dev/null +++ b/rubocop/examples/good/Style_SingleLineBlockParams.rb @@ -0,0 +1,5 @@ +# TODO: This cop checks whether the block parameters of a single-line method +# accepting a block match the names specified via configuration. + +# TODO: For instance one can configure `reduce`(`inject`) to use |a, e| +# as parameters. diff --git a/rubocop/examples/good/Style_SingleLineMethods.rb b/rubocop/examples/good/Style_SingleLineMethods.rb new file mode 100644 index 0000000..a832f45 --- /dev/null +++ b/rubocop/examples/good/Style_SingleLineMethods.rb @@ -0,0 +1,13 @@ +# Avoid single-line methods. Although they are somewhat popular in the wild, +# there are a few peculiarities about their definition syntax +# that make their use undesirable. At any rate - there should be no +# more than one expression in a single-line method. + +# good +def some_method + body +end +One exception to the rule are empty-body methods. + +# good +def no_op; end diff --git a/rubocop/examples/good/Style_SpaceAfterColon.rb b/rubocop/examples/good/Style_SpaceAfterColon.rb new file mode 100644 index 0000000..a8f48bd --- /dev/null +++ b/rubocop/examples/good/Style_SpaceAfterColon.rb @@ -0,0 +1,27 @@ +# Use spaces around operators, after commas, colons and semicolons, +# around { and before }. Whitespace might be (mostly) irrelevant to the +# Ruby interpreter, but its proper use is the key to +# writing easily readable code. + +sum = 1 + 2 +a, b = 1, 2 +[1, 2, 3].each { |e| puts e } +class FooError < StandardError; end +The only exception, regarding operators, is the exponent operator: + +# good +e = M * c**2 + +# { and } deserve a bit of clarification, since they are used for block +# and hash literals, as well as string interpolation. +# For hash literals two styles are considered acceptable. + +# good - space after { and before } +{ one: 1, two: 2 } + +# good - no space after { and before } +{one: 1, two: 2} +# The first variant is slightly more readable (and arguably more +# popular in the Ruby community in general). The second variant has +# the advantage of adding visual difference between block and hash literals. +# Whichever one you pick - apply it consistently. diff --git a/rubocop/examples/good/Style_SpaceAfterComma.rb b/rubocop/examples/good/Style_SpaceAfterComma.rb new file mode 100644 index 0000000..6afc3e4 --- /dev/null +++ b/rubocop/examples/good/Style_SpaceAfterComma.rb @@ -0,0 +1 @@ +# TODO: Checks for comma (,) not followed by some kind of space. diff --git a/rubocop/examples/good/Style_SpaceAfterMethodName.rb b/rubocop/examples/good/Style_SpaceAfterMethodName.rb new file mode 100644 index 0000000..198a33b --- /dev/null +++ b/rubocop/examples/good/Style_SpaceAfterMethodName.rb @@ -0,0 +1,4 @@ +# Do not put a space between a method name and the opening parenthesis. + +# good +f(3 + 2) + 1 diff --git a/rubocop/examples/good/Style_SpaceAfterNot.rb b/rubocop/examples/good/Style_SpaceAfterNot.rb new file mode 100644 index 0000000..50239c1 --- /dev/null +++ b/rubocop/examples/good/Style_SpaceAfterNot.rb @@ -0,0 +1,4 @@ +# No space after !. + +# good +!something diff --git a/rubocop/examples/good/Style_SpaceAfterSemicolon.rb b/rubocop/examples/good/Style_SpaceAfterSemicolon.rb new file mode 100644 index 0000000..024d859 --- /dev/null +++ b/rubocop/examples/good/Style_SpaceAfterSemicolon.rb @@ -0,0 +1,7 @@ +# TODO: Checks for semicolon (;) not followed by some kind of space. + +# good +sum = 1 + 2 +a, b = 1, 2 +[1, 2, 3].each { |e| puts e } +class FooError < StandardError; end diff --git a/rubocop/examples/good/Style_SpaceBeforeBlockBraces.rb b/rubocop/examples/good/Style_SpaceBeforeBlockBraces.rb new file mode 100644 index 0000000..9adc002 --- /dev/null +++ b/rubocop/examples/good/Style_SpaceBeforeBlockBraces.rb @@ -0,0 +1,2 @@ +# TODO: Checks that block braces have or don't have a space before +# the opening brace depending on configuration. diff --git a/rubocop/examples/good/Style_SpaceBeforeComma.rb b/rubocop/examples/good/Style_SpaceBeforeComma.rb new file mode 100644 index 0000000..2a58d0a --- /dev/null +++ b/rubocop/examples/good/Style_SpaceBeforeComma.rb @@ -0,0 +1 @@ +# TODO: Checks for comma (,) preceded by space. diff --git a/rubocop/examples/good/Style_SpaceBeforeComment.rb b/rubocop/examples/good/Style_SpaceBeforeComment.rb new file mode 100644 index 0000000..5ef51ab --- /dev/null +++ b/rubocop/examples/good/Style_SpaceBeforeComment.rb @@ -0,0 +1,4 @@ +# TODO: This cop checks for missing space between a token and a +# comment on the same line. + +# TODO: Put a space before an end-of-line comment. diff --git a/rubocop/examples/good/Style_SpaceBeforeFirstArg.rb b/rubocop/examples/good/Style_SpaceBeforeFirstArg.rb new file mode 100644 index 0000000..036f9ac --- /dev/null +++ b/rubocop/examples/good/Style_SpaceBeforeFirstArg.rb @@ -0,0 +1 @@ +# TODO: Put one space between the method name and the first argument. diff --git a/rubocop/examples/good/Style_SpaceBeforeSemicolon.rb b/rubocop/examples/good/Style_SpaceBeforeSemicolon.rb new file mode 100644 index 0000000..7525182 --- /dev/null +++ b/rubocop/examples/good/Style_SpaceBeforeSemicolon.rb @@ -0,0 +1 @@ +# TODO: Checks for semicolon (;) preceded by space. diff --git a/rubocop/examples/good/style_DoubleNegation.rb b/rubocop/examples/good/style_DoubleNegation.rb new file mode 100644 index 0000000..bd9e295 --- /dev/null +++ b/rubocop/examples/good/style_DoubleNegation.rb @@ -0,0 +1,6 @@ +# This cop checks for uses of double negation (!!) +# to convert something to a boolean value. +# As this is both cryptic and usually redundant it should be avoided. + +# good +!something.nil? diff --git a/rubocop/examples/good/style_EachWithObject.rb b/rubocop/examples/good/style_EachWithObject.rb new file mode 100644 index 0000000..d9240dc --- /dev/null +++ b/rubocop/examples/good/style_EachWithObject.rb @@ -0,0 +1,9 @@ +# This cop looks for inject / reduce calls where the passed +# in object is returned at the end and so could be replaced +# by each_with_object without the need to return the object at the end. + +# However, we can't replace with each_with_object +# if the accumulator parameter is assigned to within the block. + +# good +[1, 2].each_with_object({}) { |e, a| a[e] = e } diff --git a/rubocop/examples/good/style_EmptyLineBetweenDefs.rb b/rubocop/examples/good/style_EmptyLineBetweenDefs.rb new file mode 100644 index 0000000..a9f457e --- /dev/null +++ b/rubocop/examples/good/style_EmptyLineBetweenDefs.rb @@ -0,0 +1,15 @@ +# TODO:Use empty lines between method definitions and also +# to break up methods into logical paragraphs internally. + +# good +def some_method + data = initialize(options) + + data.manipulate! + + data.result +end + +def some_method + result +end diff --git a/rubocop/examples/good/style_EmptyLines.rb b/rubocop/examples/good/style_EmptyLines.rb new file mode 100644 index 0000000..0689f5c --- /dev/null +++ b/rubocop/examples/good/style_EmptyLines.rb @@ -0,0 +1 @@ +# TODO:This cops checks for two or more consecutive blank lines diff --git a/rubocop/examples/good/style_EmptyLinesAroundAccessModifier.rb b/rubocop/examples/good/style_EmptyLinesAroundAccessModifier.rb new file mode 100644 index 0000000..db9939d --- /dev/null +++ b/rubocop/examples/good/style_EmptyLinesAroundAccessModifier.rb @@ -0,0 +1,2 @@ +# TODO: Access modifiers should be surrounded by blank lines. +# TODO: Keep a blank line before and after `%s`.' diff --git a/rubocop/examples/good/style_EmptyLinesAroundBlockBody.rb b/rubocop/examples/good/style_EmptyLinesAroundBlockBody.rb new file mode 100644 index 0000000..22450c1 --- /dev/null +++ b/rubocop/examples/good/style_EmptyLinesAroundBlockBody.rb @@ -0,0 +1,7 @@ +# This cops checks if empty lines around +# the bodies of blocks match the configuration. + +# good +something do + ... +end diff --git a/rubocop/examples/good/style_EmptyLinesAroundClassBody.rb b/rubocop/examples/good/style_EmptyLinesAroundClassBody.rb new file mode 100644 index 0000000..ab9a9f0 --- /dev/null +++ b/rubocop/examples/good/style_EmptyLinesAroundClassBody.rb @@ -0,0 +1,9 @@ +# TODO: Checks if empty lines around the bodies +# of classes match the configuration. + +# good +class Test + def something + ... + end +end diff --git a/rubocop/examples/good/style_EmptyLinesAroundModuleBody.rb b/rubocop/examples/good/style_EmptyLinesAroundModuleBody.rb new file mode 100644 index 0000000..95af612 --- /dev/null +++ b/rubocop/examples/good/style_EmptyLinesAroundModuleBody.rb @@ -0,0 +1,8 @@ +# Checks if empty lines around the bodies of modules match the configuration. + +# good +module Test + def something + ... + end +end diff --git a/rubocop/examples/good/style_EmptyLiteral.rb b/rubocop/examples/good/style_EmptyLiteral.rb new file mode 100644 index 0000000..3f627ba --- /dev/null +++ b/rubocop/examples/good/style_EmptyLiteral.rb @@ -0,0 +1,2 @@ +# TODO: This cop checks for the use of a method, +# the result of which would be a literal, like an empty array, hash or string. diff --git a/rubocop/examples/good/style_Encoding.rb b/rubocop/examples/good/style_Encoding.rb new file mode 100644 index 0000000..e2503b4 --- /dev/null +++ b/rubocop/examples/good/style_Encoding.rb @@ -0,0 +1,3 @@ +# TODO: This cop checks whether the source file has a utf-8 encoding comment. +# This check makes sense only in Ruby 1.9, +# since in 2.0+ utf-8 is the default source file encoding. diff --git a/rubocop/examples/good/style_EndBlock.rb b/rubocop/examples/good/style_EndBlock.rb new file mode 100644 index 0000000..514a917 --- /dev/null +++ b/rubocop/examples/good/style_EndBlock.rb @@ -0,0 +1,4 @@ +# Do not use END blocks. Use Kernel#at_exit instead. + +# good +at_exit { puts 'Goodbye!' } diff --git a/rubocop/examples/good/style_EndOfLine.rb b/rubocop/examples/good/style_EndOfLine.rb new file mode 100644 index 0000000..f4fe607 --- /dev/null +++ b/rubocop/examples/good/style_EndOfLine.rb @@ -0,0 +1 @@ +# TODO: This cop checks for Windows-style line endings in the source code. diff --git a/rubocop/examples/good/style_EvenOdd.rb b/rubocop/examples/good/style_EvenOdd.rb new file mode 100644 index 0000000..222f61e --- /dev/null +++ b/rubocop/examples/good/style_EvenOdd.rb @@ -0,0 +1,5 @@ +# This cop checks for places where Fixnum#even? +# or Fixnum#odd? should have been used. + +# good +if x.even? diff --git a/rubocop/examples/good/style_FileName.rb b/rubocop/examples/good/style_FileName.rb new file mode 100644 index 0000000..5302ac9 --- /dev/null +++ b/rubocop/examples/good/style_FileName.rb @@ -0,0 +1,3 @@ +# This cop makes sure that Ruby source files have snake_case names. + +# TODO: Use snake_case for naming files, e.g. hello_world.rb. diff --git a/rubocop/examples/good/style_FlipFlop.rb b/rubocop/examples/good/style_FlipFlop.rb new file mode 100644 index 0000000..251a476 --- /dev/null +++ b/rubocop/examples/good/style_FlipFlop.rb @@ -0,0 +1 @@ +# TODO: Avoid the use of flip-flops. diff --git a/rubocop/examples/good/style_FormatString.rb b/rubocop/examples/good/style_FormatString.rb new file mode 100644 index 0000000..0c110e0 --- /dev/null +++ b/rubocop/examples/good/style_FormatString.rb @@ -0,0 +1,6 @@ +# TODO: This cop enforces the use of a single string formatting utility. +# Valid options include Kernel#format, Kernel#sprintf and String#%. + +# TODO: The detection of String#% cannot be implemented in a reliable manner +# for all cases, so only two scenarios are considered - if the first argument +# is a string literal and if the second argument is an array literal. diff --git a/rubocop/examples/good/style_GlobalVars.rb b/rubocop/examples/good/style_GlobalVars.rb new file mode 100644 index 0000000..b3f6985 --- /dev/null +++ b/rubocop/examples/good/style_GlobalVars.rb @@ -0,0 +1,10 @@ +# Use module instance variables instead of global variables. + +# good +module Foo + class << self + attr_accessor :bar + end +end + +Foo.bar = 1 diff --git a/rubocop/examples/good/style_GuardClause.rb b/rubocop/examples/good/style_GuardClause.rb new file mode 100644 index 0000000..704b5d1 --- /dev/null +++ b/rubocop/examples/good/style_GuardClause.rb @@ -0,0 +1,11 @@ +# This cop if/unless expression that can be replace with a guard clause. +# It should be extended to handle methods whose body +# is if/else or a case expression with a default branch. + +# good +def test + return unless something + work + work + work +end diff --git a/rubocop/examples/good/style_HashSyntax.rb b/rubocop/examples/good/style_HashSyntax.rb new file mode 100644 index 0000000..aa4f4b8 --- /dev/null +++ b/rubocop/examples/good/style_HashSyntax.rb @@ -0,0 +1,4 @@ +# Use the Ruby 1.9 hash literal syntax when your hash keys are symbols. + +# good +hash = { one: 1, two: 2, three: 3 } diff --git a/rubocop/examples/good/style_IfUnlessModifier.rb b/rubocop/examples/good/style_IfUnlessModifier.rb new file mode 100644 index 0000000..0f27ec7 --- /dev/null +++ b/rubocop/examples/good/style_IfUnlessModifier.rb @@ -0,0 +1,8 @@ +# Favor modifier if/unless usage when you have a single-line body. +# Another good alternative is the usage of control flow &&/||. + +# good +do_something if some_condition + +# another good option +some_condition && do_something diff --git a/rubocop/examples/good/style_IfWithSemicolon.rb b/rubocop/examples/good/style_IfWithSemicolon.rb new file mode 100644 index 0000000..fc0654c --- /dev/null +++ b/rubocop/examples/good/style_IfWithSemicolon.rb @@ -0,0 +1,4 @@ +# Do not use if x; .... Use the ternary operator instead. + +# good +result = some_condition ? something : something_else diff --git a/rubocop/examples/good/style_IndentArray.rb b/rubocop/examples/good/style_IndentArray.rb new file mode 100644 index 0000000..14b35f6 --- /dev/null +++ b/rubocop/examples/good/style_IndentArray.rb @@ -0,0 +1,6 @@ +# TODO: This cops checks the indentation of the first element in an array +# literal where the opening bracket and the first element are on separate +# lines. The other elements' indentations are handled by the AlignArray cop. + +# TODO: Array literals shall have their first element indented one step +# (2 spaces) more than the start of the line where the opening bracket is. diff --git a/rubocop/examples/good/style_IndentHash.rb b/rubocop/examples/good/style_IndentHash.rb new file mode 100644 index 0000000..1b00a59 --- /dev/null +++ b/rubocop/examples/good/style_IndentHash.rb @@ -0,0 +1,11 @@ +# TODO: This cops checks the indentation of the first key in a hash literal +# where the opening brace and the first key are on separate lines. The other keys' +# indentations are handled by the AlignHash cop + +# TODO: Hash literals that are arguments in a method call with parentheses, +# and where the opening curly brace of the hash is on the same line as the +# opening parenthesis of the method call, shall have their first key indented +# one step (two spaces) more than the position inside the opening parenthesis. + +# TODO: Other hash literals shall have their first key indented one step more +# than the start of the line where the opening curly brace is. diff --git a/rubocop/examples/good/style_IndentationConsistency.rb b/rubocop/examples/good/style_IndentationConsistency.rb new file mode 100644 index 0000000..9856fb4 --- /dev/null +++ b/rubocop/examples/good/style_IndentationConsistency.rb @@ -0,0 +1,7 @@ +# This cops checks for inconsistent indentation. + +# good +def test + puts 'hello' + puts 'world' +end diff --git a/rubocop/examples/good/style_IndentationWidth.rb b/rubocop/examples/good/style_IndentationWidth.rb new file mode 100644 index 0000000..1d00459 --- /dev/null +++ b/rubocop/examples/good/style_IndentationWidth.rb @@ -0,0 +1,6 @@ +# Use two spaces per indentation level (aka soft tabs). No hard tabs. + +# good +def some_method + do_something +end diff --git a/rubocop/examples/good/style_Lambda.rb b/rubocop/examples/good/style_Lambda.rb new file mode 100644 index 0000000..4f79145 --- /dev/null +++ b/rubocop/examples/good/style_Lambda.rb @@ -0,0 +1,6 @@ +# TODO: This cop checks for uses of the pre 1.9 lambda syntax for one-line +# anonymous functions and uses of the 1.9 lambda syntax for multi-line +# anonymous functions. + +# TODO: 'Use the new lambda literal syntax `->(params) {...}`.' +# TODO: 'Use the `lambda` method for multi-line lambdas.' diff --git a/rubocop/examples/good/style_LambdaCall.rb b/rubocop/examples/good/style_LambdaCall.rb new file mode 100644 index 0000000..0df1c48 --- /dev/null +++ b/rubocop/examples/good/style_LambdaCall.rb @@ -0,0 +1,5 @@ +# Prefer proc.call() over proc[] or proc.() for both lambdas and procs. + +# good +l = ->(v) { puts v } +l.call(1) diff --git a/rubocop/examples/good/style_LeadingCommentSpace.rb b/rubocop/examples/good/style_LeadingCommentSpace.rb new file mode 100644 index 0000000..4342ed6 --- /dev/null +++ b/rubocop/examples/good/style_LeadingCommentSpace.rb @@ -0,0 +1,4 @@ +# TODO: his cop checks whether comments have a leading space +# after the # denoting the start of the comment. +# The leading space is not required for some RDoc special syntax, +# like #++, #–, #:nodoc, etc. diff --git a/rubocop/examples/good/style_LineEndConcatenation.rb b/rubocop/examples/good/style_LineEndConcatenation.rb new file mode 100644 index 0000000..4ede290 --- /dev/null +++ b/rubocop/examples/good/style_LineEndConcatenation.rb @@ -0,0 +1,5 @@ +# This cop checks for string literal concatenation at the end of a line. + +# good +some_str = 'ala' \ + 'bala' diff --git a/rubocop/examples/good/style_LineLength.rb b/rubocop/examples/good/style_LineLength.rb new file mode 100644 index 0000000..31b51fd --- /dev/null +++ b/rubocop/examples/good/style_LineLength.rb @@ -0,0 +1 @@ +# TODO: Limit lines to 80 characters. diff --git a/rubocop/examples/good/style_MethodCallParentheses.rb b/rubocop/examples/good/style_MethodCallParentheses.rb new file mode 100644 index 0000000..77c8bb7 --- /dev/null +++ b/rubocop/examples/good/style_MethodCallParentheses.rb @@ -0,0 +1,7 @@ +# Omit parentheses for method calls with no arguments. + +# good +Kernel.exit! +2.even? +fork +'test'.upcase diff --git a/rubocop/examples/good/style_MethodDefParentheses.rb b/rubocop/examples/good/style_MethodDefParentheses.rb new file mode 100644 index 0000000..fcf8d2b --- /dev/null +++ b/rubocop/examples/good/style_MethodDefParentheses.rb @@ -0,0 +1,12 @@ +# Use def with parentheses when there are parameters. Omit the parentheses when +# the method doesn't accept any parameters. + +# good +def some_method + # body omitted +end + +# good +def some_method_with_parameters(param1, param2) + # body omitted +end diff --git a/rubocop/examples/good/style_MethodName.rb b/rubocop/examples/good/style_MethodName.rb new file mode 100644 index 0000000..95b0243 --- /dev/null +++ b/rubocop/examples/good/style_MethodName.rb @@ -0,0 +1,8 @@ +# Use snake_case for symbols, methods and variables. + +# good +:some_symbol + +def some_method + ... +end diff --git a/rubocop/examples/good/style_ModuleFunction.rb b/rubocop/examples/good/style_ModuleFunction.rb new file mode 100644 index 0000000..50e4be9 --- /dev/null +++ b/rubocop/examples/good/style_ModuleFunction.rb @@ -0,0 +1,15 @@ +# Favor the use of module_function over extend self when you want +# to turn a module's instance methods into class methods. + +# good +module Utilities + module_function + + def parse_something(string) + # do stuff here + end + + def other_utility_method(number, string) + # do some more stuff + end +end diff --git a/rubocop/examples/good/style_MultilineBlockChain.rb b/rubocop/examples/good/style_MultilineBlockChain.rb new file mode 100644 index 0000000..1b86903 --- /dev/null +++ b/rubocop/examples/good/style_MultilineBlockChain.rb @@ -0,0 +1,12 @@ +# Prefer {...} over do...end for single-line blocks. +# Avoid using {...} for multi-line blocks (multiline chaining is always ugly). +# Always use do...end for "control flow" and "method definitions" +# (e.g. in Rakefiles and certain DSLs). Avoid do...end when chaining. + +names = %w(Bozhidar Steve Sarah) + +# good +names.each { |name| puts name } + +# good +names.select { |name| name.start_with?('S') }.map(&:upcase) diff --git a/rubocop/examples/good/style_MultilineIfThen.rb b/rubocop/examples/good/style_MultilineIfThen.rb new file mode 100644 index 0000000..4b8850d --- /dev/null +++ b/rubocop/examples/good/style_MultilineIfThen.rb @@ -0,0 +1,6 @@ +# Do not use then for multi-line if/unless. + +# good +if some_condition + # body omitted +end diff --git a/rubocop/examples/good/style_MultilineTernaryOperator.rb b/rubocop/examples/good/style_MultilineTernaryOperator.rb new file mode 100644 index 0000000..35ff17c --- /dev/null +++ b/rubocop/examples/good/style_MultilineTernaryOperator.rb @@ -0,0 +1 @@ +# TODO: Avoid multi-line ?: (the ternary operator); use if/unless instead. diff --git a/rubocop/examples/good/style_NegatedIf.rb b/rubocop/examples/good/style_NegatedIf.rb new file mode 100644 index 0000000..88e090b --- /dev/null +++ b/rubocop/examples/good/style_NegatedIf.rb @@ -0,0 +1,7 @@ +# Favor unless over if for negative conditions (or control flow ||). + +# good +do_something unless some_condition + +# another good option +some_condition || do_something diff --git a/rubocop/examples/good/style_NegatedWhile.rb b/rubocop/examples/good/style_NegatedWhile.rb new file mode 100644 index 0000000..ac0abce --- /dev/null +++ b/rubocop/examples/good/style_NegatedWhile.rb @@ -0,0 +1,4 @@ +# Favor until over while for negative conditions. + +# good +do_something until some_condition diff --git a/rubocop/examples/good/style_NestedTernaryOperator.rb b/rubocop/examples/good/style_NestedTernaryOperator.rb new file mode 100644 index 0000000..71bbdc3 --- /dev/null +++ b/rubocop/examples/good/style_NestedTernaryOperator.rb @@ -0,0 +1,10 @@ +# Use one expression per branch in a ternary operator. +# This also means that ternary operators must not be nested. +# Prefer if/else constructs in these cases. [link] + +# good +if some_condition + nested_condition ? nested_something : nested_something_else +else + something_else +end diff --git a/rubocop/examples/good/style_Next.rb b/rubocop/examples/good/style_Next.rb new file mode 100644 index 0000000..fde67cf --- /dev/null +++ b/rubocop/examples/good/style_Next.rb @@ -0,0 +1,7 @@ +# Use `next` to skip iteration instead of a condition at the end. + +# good +[1, 2].each do |a| + next unless a == 1 + puts a +end diff --git a/rubocop/examples/good/style_NilComparison.rb b/rubocop/examples/good/style_NilComparison.rb new file mode 100644 index 0000000..8ca4de2 --- /dev/null +++ b/rubocop/examples/good/style_NilComparison.rb @@ -0,0 +1,18 @@ +# Favor the use of predicate methods to explicit comparisons +# with ==. Numeric comparisons are OK. + +# good +if x.even? +end + +if x.odd? +end + +if x.nil? +end + +if x.zero? +end + +if x == 0 +end diff --git a/rubocop/examples/good/style_NonNilCheck.rb b/rubocop/examples/good/style_NonNilCheck.rb new file mode 100644 index 0000000..c9f8e4f --- /dev/null +++ b/rubocop/examples/good/style_NonNilCheck.rb @@ -0,0 +1,9 @@ +# Don't do explicit non-nil checks unless you're dealing with boolean values. + +# good +do_something if something + +# good - dealing with a boolean +def value_set? + !@some_boolean.nil? +end diff --git a/rubocop/examples/good/style_Not.rb b/rubocop/examples/good/style_Not.rb new file mode 100644 index 0000000..289ba06 --- /dev/null +++ b/rubocop/examples/good/style_Not.rb @@ -0,0 +1,6 @@ +# Don't use block comments. They cannot be preceded by whitespace and +# are not as easy to spot as regular comments. [link] + +# good +# comment line +# another comment line diff --git a/rubocop/examples/good/style_NumericLiterals.rb b/rubocop/examples/good/style_NumericLiterals.rb new file mode 100644 index 0000000..ce3cba9 --- /dev/null +++ b/rubocop/examples/good/style_NumericLiterals.rb @@ -0,0 +1,2 @@ +# TODO: his cop checks for big numeric literals +# without _ between groups of digits in them. diff --git a/rubocop/examples/good/style_OneLineConditional.rb b/rubocop/examples/good/style_OneLineConditional.rb new file mode 100644 index 0000000..1c56874 --- /dev/null +++ b/rubocop/examples/good/style_OneLineConditional.rb @@ -0,0 +1,5 @@ +# Favor the ternary operator(?:) over if/then/else/end constructs. +# It's more common and obviously more concise. [link] + +# good +result = some_condition ? something : something_else diff --git a/rubocop/examples/good/style_OpMethod.rb b/rubocop/examples/good/style_OpMethod.rb new file mode 100644 index 0000000..c8fcaa0 --- /dev/null +++ b/rubocop/examples/good/style_OpMethod.rb @@ -0,0 +1,6 @@ +# When defining binary operators, name the parameter other(<< and [] are +# exceptions to the rule, since their semantics are different). [link] + +def +(other) + # body omitted +end diff --git a/rubocop/examples/good/style_ParenthesesAroundCondition.rb b/rubocop/examples/good/style_ParenthesesAroundCondition.rb new file mode 100644 index 0000000..c3fb7f9 --- /dev/null +++ b/rubocop/examples/good/style_ParenthesesAroundCondition.rb @@ -0,0 +1,6 @@ +# Don't use parentheses around the condition of an if/unless/while/until. + +# good +if x > 10 + # body omitted +end diff --git a/rubocop/examples/good/style_PercentLiteralDelimiters.rb b/rubocop/examples/good/style_PercentLiteralDelimiters.rb new file mode 100644 index 0000000..ef783fd --- /dev/null +++ b/rubocop/examples/good/style_PercentLiteralDelimiters.rb @@ -0,0 +1 @@ +# TODO: This cop enforces the consitent useage of `%`-literal delimiters. diff --git a/rubocop/examples/good/style_PerlBackrefs.rb b/rubocop/examples/good/style_PerlBackrefs.rb new file mode 100644 index 0000000..7518212 --- /dev/null +++ b/rubocop/examples/good/style_PerlBackrefs.rb @@ -0,0 +1,8 @@ +# Don't use the cryptic Perl-legacy variables denoting last regexp group matches +# ($1, $2, etc). Use Regexp.last_match(n) instead. + +/(regexp)/ =~ string +... + +# good +process Regexp.last_match(1) diff --git a/rubocop/examples/good/style_PredicateName.rb b/rubocop/examples/good/style_PredicateName.rb new file mode 100644 index 0000000..a46e7d0 --- /dev/null +++ b/rubocop/examples/good/style_PredicateName.rb @@ -0,0 +1,3 @@ +# TODO: The names of predicate methods (methods that return a boolean value) +# should end in a question mark. (i.e. Array#empty?). Methods that don't +# return a boolean, shouldn't end in a question mark diff --git a/rubocop/examples/good/style_Proc.rb b/rubocop/examples/good/style_Proc.rb new file mode 100644 index 0000000..496e77f --- /dev/null +++ b/rubocop/examples/good/style_Proc.rb @@ -0,0 +1,4 @@ +# Prefer proc over Proc.new. + +# good +p = proc { |n| puts n } diff --git a/rubocop/examples/good/style_RaiseArgs.rb b/rubocop/examples/good/style_RaiseArgs.rb new file mode 100644 index 0000000..1e1e1d9 --- /dev/null +++ b/rubocop/examples/good/style_RaiseArgs.rb @@ -0,0 +1 @@ +# TODO: This cop checks the args passed to `fail` and `raise`. diff --git a/rubocop/examples/good/style_RedundantBegin.rb b/rubocop/examples/good/style_RedundantBegin.rb new file mode 100644 index 0000000..98efe42 --- /dev/null +++ b/rubocop/examples/good/style_RedundantBegin.rb @@ -0,0 +1 @@ +# TODO: This cop checks for redundant begin blocks diff --git a/rubocop/examples/good/style_RedundantException.rb b/rubocop/examples/good/style_RedundantException.rb new file mode 100644 index 0000000..45af800 --- /dev/null +++ b/rubocop/examples/good/style_RedundantException.rb @@ -0,0 +1,4 @@ +# Don't specify RuntimeError explicitly in the two argument version of raise. + +# good - signals a RuntimeError by default +raise 'message' diff --git a/rubocop/examples/good/style_RedundantReturn.rb b/rubocop/examples/good/style_RedundantReturn.rb new file mode 100644 index 0000000..0fd91e7 --- /dev/null +++ b/rubocop/examples/good/style_RedundantReturn.rb @@ -0,0 +1,6 @@ +# Avoid return where not required for flow of control. + +# good +def some_method(some_arr) + some_arr.size +end diff --git a/rubocop/examples/good/style_RedundantSelf.rb b/rubocop/examples/good/style_RedundantSelf.rb new file mode 100644 index 0000000..e643e9f --- /dev/null +++ b/rubocop/examples/good/style_RedundantSelf.rb @@ -0,0 +1,7 @@ +# TODO: This cop checks for redundant uses of `self`. + +# `self` is only needed when: +# Sending a message to same object with zero arguments in presence of a method +# name clash with an argument or a local variable. +# Note, with using explicit self you can only send messages with public +# or protected scope, you cannot send private messages this way. diff --git a/rubocop/examples/good/style_RegexpLiteral.rb b/rubocop/examples/good/style_RegexpLiteral.rb new file mode 100644 index 0000000..525ce9e --- /dev/null +++ b/rubocop/examples/good/style_RegexpLiteral.rb @@ -0,0 +1,5 @@ +# Use %r only for regular expressions matching at least one '/' character. + +# good +%r{^/(.*)$} +%r{^/blog/2011/(.*)$} diff --git a/rubocop/examples/good/style_RescueModifier.rb b/rubocop/examples/good/style_RescueModifier.rb new file mode 100644 index 0000000..04cfb38 --- /dev/null +++ b/rubocop/examples/good/style_RescueModifier.rb @@ -0,0 +1 @@ +# Avoid using `rescue` in its modifier form. diff --git a/rubocop/examples/good/style_for.rb b/rubocop/examples/good/style_for.rb new file mode 100644 index 0000000..11dd1fe --- /dev/null +++ b/rubocop/examples/good/style_for.rb @@ -0,0 +1,14 @@ +# Do not use for, unless you know exactly why. +# Most of the time iterators should be used instead. +# for is implemented in terms of each +# (so you're adding a level of indirection), +# but with a twist - for doesn't introduce a new scope (unlike each) +# and variables defined in its block will be visible outside it. + +arr = [1, 2, 3] + +# good +arr.each { |elem| puts elem } + +# elem is not accessible outside each's block +elem # => NameError: undefined local variable or method `elem'