# bad SomeClass::some_method some_object::some_method # good SomeClass.some_method some_object.some_method SomeModule::SomeClass::SOME_CONST SomeModule::SomeClass()
# bad
def some_method()
# body omitted
end
# good
def some_method
# body omitted
end
# bad
def some_method_with_arguments arg1, arg2
# body omitted
end
# good
def some_method_with_arguments(arg1, arg2)
# body omitted
end
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
# good
arr.each { |elem| puts elem }
# elem is not accessible outside each's block
elem #=> NameError: undefined local variable or method `elem'
# bad if some_condition then # body omitted end # good if some_condition # body omitted end
# bad if some_condition do_something do_something_else end # good if some_condition do_something do_something_else end
# bad result = if some_condition then something else something_else end # good result = some_condition ? something : something_else
# bad some_condition ? (nested_condition ? nested_something : nested_something_else) : something_else # good if some_condition nested_condition ? nested_something : nested_something_else else something_else end
# bad result = if some_condition then something else something_else end # good result = some_condition ? something : something_else
# bad
if condition
result = x
else
result = y
end
# good
result =
if condition
x
else
y
end
# 差 - 因为操作符有优先级,需要用括号。 x = (not something) # good x = !something 避免使用 !!. # bad x = 'test' # obscure nil check if !!x # body omitted end x = false # double negation is useless on booleans !!x # => false # good x = 'test' unless x.nil? # body omitted end
# bad # boolean expression if some_condition and some_other_condition do_something end # control flow document.saved? or document.save! # good # boolean expression if some_condition && some_other_condition do_something end # control flow
# bad if some_condition do_something end # good do_something if some_condition # another good option some_condition && do_something
# boolean expression if some_condition && some_other_condition do_something end # control flow document.save? or document.save!
# bad if some_condition do_something end # good do_something if some_condition # another good option some_condition and do_something
# bad unless success? puts 'failure' else puts 'success' end # good if success? puts 'success' else puts 'failure' end
# bad if (x > 10) # body omitted end # good if x > 10 # body omitted end
# 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 while some_condition do_something end # good do_something while some_condition
# bad do_something while !some_condition # good do_something until some_condition
# bad begin puts val val += 1 end while val < 0 # good loop do puts val val += 1 break unless val < 0 end
class Person
attr_reader :name, :age
# omitted
end
temperance = Person.new('Temperance', 30)
temperance.name
puts temperance.age
x = Math.sin(y)
array.delete(e)
bowling.score.should == 0
# bad
user.set({ name: 'John', age: 45, permissions: { read: true } })
# good
user.set(name: 'John', age: 45, permissions: { read: true })
class Person < ActiveRecord::Base
# bad
validates(:name, { presence: true, length: { within: 1..10 } })
# good
validates :name, presence: true, length: { within: 1..10 }
end
# bad Kernel.exit!() 2.even?() fork() 'test'.upcase() # good Kernel.exit! 2.even? fork 'test'.upcase
names = ['Bozhidar', 'Steve', 'Sarah']
# bad
names.each do |name|
puts name
end
# good
names.each { |name| puts name }
# bad
names.select do |name|
name.start_with?('S')
end.map { |name| name.upcase }
# good
names.select { |name| name.start_with?('S') }.map { |name| name.upcase }
require 'tempfile'
# bad
def with_tmp_dir
Dir.mktmpdir do |tmp_dir|
Dir.chdir(tmp_dir) { |dir| yield dir } # block just passes arguments
end
end
# good
def with_tmp_dir(&block)
Dir.mktmpdir do |tmp_dir|
Dir.chdir(tmp_dir, &block)
end
end
with_tmp_dir do |dir|
puts "dir is accessible as parameter and pwd is set: #{dir}"
end
# bad def some_method(some_arr) return some_arr.size end # good def some_method(some_arr) some_arr.size end
# bad
def ready?
if self.last_reviewed_at > self.last_updated_at
self.worker.update(self.content, self.options)
self.status = :in_progress
end
self.status == :verified
end
# good
def ready?
if last_reviewed_at > last_updated_at
worker.update(content, options)
self.status = :in_progress
end
status == :verified
end
class Foo
attr_accessor :options
# ok
def initialize(options)
self.options = options
# both options and self.options are equivalent here
end
# bad
def do_something(options = {})
unless options[:when] == :later
output(self.options[:message])
end
end
# good
def do_something(params = {})
unless params[:when] == :later
output(options[:message])
end
end
end
# bad (+ a warning) if v = array.grep(/foo/) do_something(v) ... end # good (MRI would still complain, but RuboCop won't) if (v = array.grep(/foo/)) do_something(v) ... end # good v = array.grep(/foo/) if v do_something(v) ... end
# bad x = x + y x = x * y x = x**y x = x / y x = x || y x = x && y # good x += y x *= y x **= y x /= y x ||= y x &&= y
# set name to Vozhidar, only if it's nil or false name ||= 'Bozhidar'
# bad - would set enabled to true even if it was false enable ||= true # good enabled = true if enabled.nil?
# bad if something something = something.downcase end # bad something = something ? nil : something.downcase # ok something = something.downcase if something # good something = something && something.downcase # better something &&= something.downcase
# bad Array === something (1..100) === 7 /something/ === some_string # good something.is_a?(Array) (1..100).include?(7) some_string =~ /something/
# bad $:.unshift File.dirname(__FILE__) # good require 'English' $LOAD_PATH.unshift File.dirname(__FILE__) 从来不要在方法名和(参数)开括号之间使用空格。 # bad f (3+2) + 1 # good f(3 + 2) +1
# bad
l = lambda { |a, b| a + b }
l.call(1, 2)
# correct, but looks extremely awkward
l = ->(a, b) do
tmp = a * 7
tmp * b / 50
end
# good
l = ->(a, b) { a + b }
l.call(1, 2)
l = lambda do |a, b|
tmp = a * 7
tmp * b / 50
end
# bad
p = Proc.new { |n| puts n }
# good
p = proc { |n| puts n }
匿名方法 和 块 用 proc.call() 而不是 proc[] 或 proc.()。
# bad - looks similar to Enumeration access
l = ->(v) { puts v }
l[1]
# also bad - uncommon syntax
l = ->(v) { puts v }
l.(1)
# good
l = ->(v) { puts v }
l.call(1)
# bad
result = hash.map { |k, v| v + 1 }
def something(x)
unused_var, used_var = something_else(x)
# ...
end
# good
result = hash.map { |_k, v| v + 1 }
def something(x)
_unused_var, used_var = something_else(x)
# ...
end
# good
result = hash.map { |_, v| v + 1 }
def something(x)
_, used_var = something_else(x)
# ...
end
# bad
'%d %d' % [20, 10]
# => '20 10'
# good
sprintf('%d %d', 20, 10)
# => '20 10'
# good
sprintf('%{first} %{second}', first: 20, second: 10)
# => '20 10'
format('%d %d', 20, 10)
# => '20 10'
# good
format('%{first} %{second}', first: 20, second: 10)
# => '20 10'
# bad
%w(one two three) * ', '
# => 'one, two, three'
# good
%w(one two three).join(', ')
# => 'one, two, three'
# bad
paths = [paths] unless paths.is_a? Array
paths.each { |path| do_something(path) }
# good
[*paths].each { |path| do_something(path) }
# good (and a bit more readable)
Array(paths).each { |path| do_something(path) }
# bad do_something if x >= 1000 && x <= 2000 # good do_something if (1000..2000).include?(x) # good do_something if x.between?(1000, 2000)
# bad if x % 2 == 0 end if x % 2 == 1 end if x == nil end # good if x.even? end if x.odd? end if x.nil? end if x.zero? end if x == 0 end
# bad
END { puts 'Goodbye!' }
# good
at_exit { puts 'Goodbye!' }
# bad
def compute_thing(thing)
if thing[:foo]
update_with_bar(thing)
if thing[:foo][:bar]
partial_compute(thing)
else
re_compute(thing)
end
end
end
# good
def compute_thing(thing)
return unless thing[:foo]
update_with_bar(thing[:foo])
return re_compute(thing) unless thing[:foo][:bar]
partial_compute(thing)
end
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有