kitaro-tn
4/9/2016 - 6:01 AM

Rubocopチートシート ref: http://qiita.com/kitaro_tn/items/abb881c098b3df3f9871

# bad
def set_attribute(value) ...

# good
def attribute=(value)

# bad
def get_attribute ...

# good
def attribute ...
def my_method                   # 1
  if cond                       # 1
    case var                    # 2 (0.8 + 4 * 0.2, rounded)
    when 1 then func_one
    when 2 then func_two
    when 3 then func_three
    when 4..10 then func_other
    end
  else                          # 1
    do_something until a && b   # 2
  end                           # ===
end                             # 7 complexity points
def count(num)
  count = num + 1
end
do_something do |used, unused, _unused_but_allowed|
  puts used
end
hash = { food: 'apple', food: 'orange' }
# bad
"#{@var}"

# good
@var.to_s

# good if @var is already a String
@var
# always bad
a = [1, 2,]

# good if EnforcedStyleForMultiline is consistent_comma
a = [
  1, 2,
  3,
]

# good if EnforcedStyleForMultiline is comma or consistent_comma
a = [
  1,
  2,
]

# good if EnforcedStyleForMultiline is no_comma
a = [
  1,
  2
]
# bad
something.map { |s| s.upcase }

# good
something.map(&:upcase)
attr_writer :bar

def foo self.bar= 1 # Make sure above attr writer is called end
def bar :baz end

def foo(bar) self.bar # resolves name clash with argument end

def foo2 bar = 1 self.bar # resolves name clash with local variable end
variable = lambda do |i|
  i
end
# bad
(x) if ((y.z).nil?)

# good
x if y.z.nil?
# bad
CONST = 1.freeze

# good
CONST = 1
def redundant
  begin
    ala
    bala
  rescue StandardError => e
    something
  end
end

def preferred
  ala
  bala
rescue StandardError => e
  something
end
'%':  ()
'%i': ()
'%q': ()
'%Q': ()
'%r': '{}'
'%s': ()
'%w': ()
'%W': ()
'%x': ()
# bad
CONST = [1, 2, 3]

# good
CONST = [1, 2, 3].freeze
# bad
blah do |i| foo(i)
  bar(i)
end

# bad
blah do
  |i| foo(i)
  bar(i)
end

# good
blah do |i|
  foo(i)
  bar(i)
end

# bad
blah { |i| foo(i)
  bar(i)
}

# good
blah { |i|
  foo(i)
  bar(i)
}
module Test
  extend self

  ...
class A
 def test
  puts 'hello'
 end
end
# bad
def test
  if something
    work
  end
end

# good
def test
  return unless something
  work
end

# also good
def test
  work if something
end

# bad
if something
  raise 'exception'
else
  ok
end

# good
raise 'exception' if something
ok
# bad
some_method(
first_param,
second_param)

# good
some_method(
  first_param,
second_param)
if a = "hoge"
  puts a
end
# good if AllowForAlignment is true
name      = "RuboCop"
# Some comment and an empty line

website  += "/bbatsov/rubocop" unless cond
puts        "rubocop"          if     debug

# bad for any configuration
set_app("RuboCop")
website  = "https://github.com/bbatsov/rubocop"
module Test

   def something
     ...
   end

end
def something(arg)

  ...
end
class Test

   def something
     ...
   end

end
# bad
[1, 2].inject({}) { |a, e| a[e] = e; a }

# good
[1, 2].each_with_object({}) { |e, a| a[e] = e }

# bad
!!something

# good
!something.nil?
# bad
if foo
  bar = 1
else
  bar = 2
end

case foo
when 'a'
  bar += 1
else
  bar += 2
end

if foo
  some_method
  bar = 1
else
  some_other_method
  bar = 2
end

# good
bar = if foo
        1
      else
        2
      end

bar += case foo
       when 'a'
         1
       else
         2
       end

bar << if foo
         some_method
         1
       else
         some_other_method
         2
       end
# good: when x is on its own line, indent this way
func(
  x,
  y
)

# good: when x follows opening parenthesis, align parentheses
a = b * (x +
         y
        )

# bad
def func(
  x,
  y
  )
# bad
blah do |i|
  foo(i) end

# good
blah do |i|
  foo(i)
end

# bad
blah { |i|
  foo(i) }

# good
blah { |i|
  foo(i)
}
# This is interpreted as a method invocation with a regexp literal,
# but it could possibly be `/` method invocations.
# (i.e. `do_something./(pattern)./(i)`)
do_something /pattern/i

# With parentheses, there's no ambiguity.
do_something(/pattern/i)
array = [1, 2, 3]

# The `*` is interpreted as a splat operator but it could possibly be
# a `*` method invocation (i.e. `do_something.*(array)`).
do_something *array

# With parentheses, there's no ambiguity.
do_something(*array)