方法
实例公共方法
class_attribute(*attrs, instance_accessor: true, instance_reader: instance_accessor, instance_writer: instance_accessor, instance_predicate: true, default: nil ) 链接
声明一个类级别的属性,其值可由子类继承。子类可以更改自己的值,而不会影响父类。
选项¶ ↑
-
:instance_reader- 设置实例读取器方法(默认为 true)。 -
:instance_writer- 设置实例写入器方法(默认为 true)。 -
:instance_accessor- 设置两个实例方法(默认为 true)。 -
:instance_predicate- 设置一个谓词方法(默认为 true)。 -
:default- 为属性设置默认值(默认为 nil)。
示例¶ ↑
class Base class_attribute :setting end class Subclass < Base end Base.setting = true Subclass.setting # => true Subclass.setting = false Subclass.setting # => false Base.setting # => true
在上述情况下,只要子类没有通过执行 Subclass.setting = something 为 setting 赋值,Subclass.setting 就会读取父类赋给它的值。一旦子类赋值,就会返回子类赋给它的值。
这与正常的 Ruby 方法继承相匹配:将子类中的属性视为覆盖读取器方法。但是,在使用 class_attribute 处理可变结构(如 Array 或 Hash)时,您需要注意。在这种情况下,您不希望进行原地更改。而是使用 setter。
Base.setting = [] Base.setting # => [] Subclass.setting # => [] # Appending in child changes both parent and child because it is the same object: Subclass.setting << :foo Base.setting # => [:foo] Subclass.setting # => [:foo] # Use setters to not propagate changes: Base.setting = [] Subclass.setting += [:foo] Base.setting # => [] Subclass.setting # => [:foo]
为了方便起见,还定义了一个实例谓词方法。要跳过它,请传递 instance_predicate: false。
Subclass.setting? # => false
实例也可以以同样的方式覆盖类值。
Base.setting = true object = Base.new object.setting # => true object.setting = false object.setting # => false Base.setting # => true
要选择退出实例读取器方法,请传递 instance_reader: false。
object.setting # => NoMethodError object.setting? # => NoMethodError
要选择退出实例写入器方法,请传递 instance_writer: false。
object.setting = false # => NoMethodError
要选择退出两个实例方法,请传递 instance_accessor: false。
要为属性设置默认值,请传递 default:,如下所示。
class_attribute :settings, default: {}
源代码: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/class/attribute.rb, line 86 def class_attribute(*attrs, instance_accessor: true, instance_reader: instance_accessor, instance_writer: instance_accessor, instance_predicate: true, default: nil ) class_methods, methods = [], [] attrs.each do |name| unless name.is_a?(Symbol) || name.is_a?(String) raise TypeError, "#{name.inspect} is not a symbol nor a string" end name = name.to_sym namespaced_name = :"__class_attr_#{name}" ::ActiveSupport::ClassAttribute.redefine(self, name, namespaced_name, default) class_methods << "def #{name}; #{namespaced_name}; end" class_methods << "def #{name}=(value); self.#{namespaced_name} = value; end" if singleton_class? methods << <<~RUBY if instance_reader silence_redefinition_of_method(:#{name}) def #{name} self.singleton_class.#{name} end RUBY else methods << <<~RUBY if instance_reader silence_redefinition_of_method def #{name} if defined?(@#{name}) @#{name} else self.class.#{name} end end RUBY end methods << <<~RUBY if instance_writer silence_redefinition_of_method(:#{name}=) attr_writer :#{name} RUBY if instance_predicate class_methods << "silence_redefinition_of_method def #{name}?; !!self.#{name}; end" if instance_reader methods << "silence_redefinition_of_method def #{name}?; !!self.#{name}; end" end end end location = caller_locations(1, 1).first class_eval(["class << self", *class_methods, "end", *methods].join(";").tr("\n", ";"), location.path, location.lineno) end
descendants() 链接
返回一个包含接收者所有子类的数组。
class C; end C.descendants # => [] class B < C; end C.descendants # => [B] class A < B; end C.descendants # => [B, A] class D < C; end C.descendants # => [B, A, D]
源代码: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/class/subclasses.rb, line 19 def descendants subclasses.concat(subclasses.flat_map(&:descendants)) end