是否有关于类变量,实例变量等的空间的指南?例如
class MyModel < ApplicationRecord
belongs_to :something
has_many: :something_elses
validates: :property, presence: true
after_save :do_something
end
在这种情况下,我以模型记录为例,但我想了解所有内容的标准样式。我正在使用Rubocop,它什么也没有告诉我。
谢谢。
[anothermh分享了上面的链接(为了记录下来,当这个问题是关于Rubocop时,令人惊讶的是他们的个人资料照片是Robocop的……)但这是Rubocop guidelines for classes的建议:
class Person
# extend and include go first
extend SomeModule
include AnotherModule
# inner classes
CustomError = Class.new(StandardError)
# constants are next
SOME_CONSTANT = 20
# afterwards we have attribute macros
attr_reader :name
# followed by other macros (if any)
validates :name
# public class methods are next in line
def self.some_method
end
# initialization goes between class methods and other instance methods
def initialize
end
# followed by other public instance methods
def some_method
end
# protected and private methods are grouped near the end
protected
def some_protected_method
end
private
def some_private_method
end
end
[作为个人说明:尽管具有一致的样式,它使读取代码和其他人扫描所写内容的速度更快,更轻松,但是请记住,这些只是“最佳做法”的建议。归根结底,最适合您的应该是[[您的新的最佳实践。