Skip to content

Instantly share code, notes, and snippets.

@YumaInaura
Last active May 24, 2016 05:02
Show Gist options
  • Save YumaInaura/d6154e5a3b261a065ab2 to your computer and use it in GitHub Desktop.
Save YumaInaura/d6154e5a3b261a065ab2 to your computer and use it in GitHub Desktop.
Rubocop がチェックしてくれるもの一覧表 (Style編) ref: http://qiita.com/Yinaura/items/9928e32fa9fc9092f24c
#例:
Style/RegexpLiteral:
EnforcedStyle: percent_r
# bad
class SomeClass
private
def private_method
# ...
end
end
# good
class SomeClass
private
def private_method
# ...
end
end
# bad
def set_attribute(value) ...
# good
def attribute=(value)
# bad
def get_attribute ...
# good
def attribute ...
# bad
$ORIGINAL_PATH = '/path/to/user'
puts $ORIGINAL_PATH
# good
puts $LOAD_PATH
# bad
hash = { :kind => 'Apple' }
# good
hash = { kind: 'Apple' }
def test
puts 'hello'
- puts 'world'
+ puts 'world'
end
# bad
def test
puts 'hello'
end
# good
def test
puts 'hello'
end
# bad
if cond then
end
# good
if cond
end
# good
if cond then a
elsif cond then b
end
- expect{ subject }
+ expect { subject }
# bad
true ?
yes :
no
# good
true ? yes : no
# good
if true
yes
else
no
end
# bad
some_str = 'ala' +
'bala'
some_str = 'ala' <<
'bala'
# good
some_str = 'ala' \
'bala'
# bad
def some_method()
# body omitted
end
# good
def some_method
# body omitted
end
# bad
def some_method_with_parameters param1, param2
# body omitted
end
# good
def some_method_with_parameters(param1, param2)
# body omitted
end
# bad
def ExampleMethod
end
# good
def example_method
end
# 悪い例
%w(one two three) * ', '
# => 'one, two, three'
# 良い例
%w(one two three).join(', ')
# => 'one, two, three'
# bad
if x != nil
if !x.nil?
# good
if x
# good
def signed_in?
!current_user.nil?
end
# bad
if (a && b)
end
# good
if a && b
end
# good
if (a && b) || c
end
# bad
def is_even?(value) ...
# good
def even?(value)
# bad
def has_value? ...
# good
def value? ...
def redundant
begin
ala
bala
rescue StandardError => e
something
end
end
def preferred
ala
bala
rescue StandardError => e
something
end
# bad
raise RuntimeError, 'message'
# good - signals a RuntimeError by default
raise 'message'
# maybe bad
def test
return something
end
# maybe good
def test
something
end
# bad
begin
something
rescue
puts 'error'
end
# good
begin
something
rescue
puts 'error'
end
# EnforcedStyle の設定が slashes / mixed の場合は良い。 percent_r の場合は悪い。
snake_case = /^[\dA-Z_]+$/
# EnforcedStyle の設定が percent_r の場合は良い。 slashes / mixed の場合は悪い。
snake_case = %r{^[\dA-Z_]+$}
# EnforcedStyle の設定が slashes の場合は良い。 percent_r / mixed の場合は悪い。
regex = /
foo
(bar)
(baz)
/x
# EnforcedStyle の設定が percent_r / mixed の場合は良い。 slashes の場合は悪い。
regex = %r{
foo
(bar)
(baz)
}x
# スラッシュ記法の中でスラッシュを使わないこと。
# AllowInnerSlashes の設定を true にしていれば警告しない。
x =~ /home\//
# EnforcedStyle の設定が slashes / mixed の場合は良い。 percent_r の場合は悪い。
snake_case = /^[\dA-Z_]+$/
# EnforcedStyle の設定が percent_r の場合は良い。 slashes / mixed の場合は悪い。
snake_case = %r{^[\dA-Z_]+$}
# EnforcedStyle の設定が slashes の場合は良い。 percent_r / mixed の場合は悪い。
regex = /
foo
(bar)
(baz)
/x
# EnforcedStyle の設定が percent_r / mixed の場合は良い。 slashes の場合は悪い。
regex = %r{
foo
(bar)
(baz)
}x
# スラッシュ記法の中でスラッシュを使わないこと。
# AllowInnerSlashes の設定を true にしていれば警告しない。
x =~ /home\//
# bad
if x = 1
end
# bad
x = x + 1
# good
x += 1
# bad
def method; nil; end
# good
def method
nil
end
# bad
x = 1;y = 2
# good
x = 1; y = 2
# bad
hash.map! {|k, v|v }
# bad
hash.map { | k, v | v }
# good
hash.map { |k, v| v }
#bad
['Apple','Banana','Strawberry']
#good
['Apple', 'Banana', 'Strawberry']
# bad
if(a && b) || c then result end
# good_
if (a && b) || c then result end
# bad
def func (x)
end
# good
def func(x)
end
# bad
! something
# good
!something
# bad
x = 1;y = 2;
# good
x = 1; y = 2;
# bad
def example(value='')
end
# good
def example(value = '')
end
# 例
attr_accessor :getter_with_setter
attr_reader :getter1, :getter2, :getter3
attr_writer :setter1, :setter2, :setter3
# bad
x=1+1
# bad
x = 1 + 1
# good
x = 1 + 1
# bad
x = 2 ** 3
# good
x = 2**3
# bad
(1..10).each{ |i| i }
# good
(1..10).each { |i| i }
# bad
method# Comment
# good
method # Comment
# bad
result if(a && b) || c
# good
result if (a && b) || c
# bad
(1..10).each {|i| i}
# good
(1..10).each { |i| i }
# bad
furuits = {apple: 10, banana: 20}
# good
furuits = { apple: 10, banana: 20 }
# bad
some( arg ).other
[ 1, 2, 3 ].size
# good
some(arg).other
[1, 2, 3].size
# bad
1 .. 3
# good
1..3
# bad
'a' .. 'z'
# good
'a'..'z'
$: => $LOAD_PATH
$" => $LOADED_FEATURES
$0 => $PROGRAM_NAME
$! => $ERROR_INFO
$@ => $ERROR_POSITION
$; => $FIELD_SEPARATOR / $FS
$, => $OUTPUT_FIELD_SEPARATOR / $OFS
$/ => $INPUT_RECORD_SEPARATOR / $RS
$\\ => $OUTPUT_RECORD_SEPARATOR / $ORS
$. => $INPUT_LINE_NUMBER / $NR
$_ => $LAST_READ_LINE
$> => $DEFAULT_OUTPUT
$< => $DEFAULT_INPUT
$$ => $PROCESS_ID / $PID
$? => $CHILD_STATUS
$~ => $LAST_MATCH_INFO
$= => $IGNORECASE
$* => $ARGV => / => ARGV
$& => $MATCH
$` => $PREMATCH
$\' => $POSTMATCH
$+ => $LAST_PAREN_MATCH
# 悪い例
== begin
comment line
another comment line
== end
# 良い例
# comment line
# another comment line
# bad
:symbol"
# good
:symbol
# bad
something.map { |s| s.upcase }
# good
something.map(&:upcase)
# maybe bad
x_ = 1
# bad
class Foo
def example=(value)
@example = value
end
def example
@example
end
end
# good
class Foo
attr_accessor :example
end
# bad
someVar = 5
# good
some_var = 5
# bad
when x; apple
# good
when x then apple
# bad
while x > 5 do
# body omitted
end
until x > 5 do
# body omitted
end
# good
while x > 5
# body omitted
end
until x > 5
# body omitted
end
# bad
class Example
def some_method(argument1, argument2, argument3, argument4, argument5, argument6)
end
end
# 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
# 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
# bad
#FIXME Comment
# good
# FIXME: Comment
some_array = []
some_hash = {}
some_string = ''
# bad
if x % 2 == 0
# good
if x.even?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment