class variable and class instance variable and class_attribute in Ruby

要点概览

  • 类变量(class variable, @@var_name)
  • 类实例变量(class instance variable, @var_name)
  • 类属性(由class_attribute定义,Rails中提供,同时提供了类实例变量和实例变量以及其accessor)

class variable

类似全局变量,很少用。

#
class A
  @@kind = "animal"
end

class attribute

require 'rails'
class A
  class_attribute :kind
end

A.kind
A.kind = 1
# 实例中a的@kind在未初始化时,取值为类A实例变量@kind的值
a = A.new

a.kind
a.kind = "some"


class B < A
end

# A.kind 与 B.kind独立使用,如果B.kind未初始化,则取值为A.kind
B.kind
b = B.new
b.kind

class instance variable

# example 1
class G
  @x = ""
  def self.x
    @x
  end
  def self.x=(v)
    @x=v
  end
end
G.instance_variables
#=> [:@x]


# example 2
class H
  class_attribute :x
end

H.instance_variables
# => []
H.class_variables
#=> []
H.singleton_class.instance_variables
# => []
H.singleton_class.class_variables
# => []

class_attribute不属于任何系统预定义的变量范畴。

class variable,格式:@@var_name,值绑定在类定义上,类似全局变量,极少使用。

class_attribute,格式:@var_name,定义了类实例变量和实例变量,及其各自的attr_accessor。类实例变量位于单件类中。实例变量位于实例中。类实例变量可以被继承,但使用起来相互独立。值在超类和子类间独立。

源码详解

require "active_support/core_ext/kernel/singleton_class"
require "active_support/core_ext/module/remove_method"
require "active_support/core_ext/array/extract_options"

class Class
  # Declare a class-level attribute whose value is inheritable by subclasses.
  # Subclasses can change their own value and it will not impact parent class.
  #
  #   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
  #
  # In the above case as long as Subclass does not assign a value to setting
  # by performing <tt>Subclass.setting = _something_</tt>, <tt>Subclass.setting</tt>
  # would read value assigned to parent class. Once Subclass assigns a value then
  # the value assigned by Subclass would be returned.
  #
  # This matches normal Ruby method inheritance: think of writing an attribute
  # on a subclass as overriding the reader method. However, you need to be aware
  # when using +class_attribute+ with mutable structures as +Array+ or +Hash+.
  # In such cases, you don't want to do changes in place. Instead use setters:
  #
  #   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]
  #
  # For convenience, an instance predicate method is defined as well.
  # To skip it, pass <tt>instance_predicate: false</tt>.
  #
  #   Subclass.setting?       # => false
  #
  # Instances may overwrite the class value in the same way:
  #
  #   Base.setting = true
  #   object = Base.new
  #   object.setting          # => true
  #   object.setting = false
  #   object.setting          # => false
  #   Base.setting            # => true
  #
  # To opt out of the instance reader method, pass <tt>instance_reader: false</tt>.
  #
  #   object.setting          # => NoMethodError
  #   object.setting?         # => NoMethodError
  #
  # To opt out of the instance writer method, pass <tt>instance_writer: false</tt>.
  #
  #   object.setting = false  # => NoMethodError
  #
  # To opt out of both instance methods, pass <tt>instance_accessor: false</tt>.
  def class_attribute(*attrs)
    options = attrs.extract_options!
    instance_reader = options.fetch(:instance_accessor, true) && options.fetch(:instance_reader, true)
    instance_writer = options.fetch(:instance_accessor, true) && options.fetch(:instance_writer, true)
    instance_predicate = options.fetch(:instance_predicate, true)

    attrs.each do |name|
      remove_possible_singleton_method(name)
      define_singleton_method(name) { nil }

      remove_possible_singleton_method("#{name}?")
      define_singleton_method("#{name}?") { !!public_send(name) } if instance_predicate

      ivar = "@#{name}"

      remove_possible_singleton_method("#{name}=")
      define_singleton_method("#{name}=") do |val|
        # 定义类方法
        singleton_class.class_eval do
          remove_possible_method(name)
          define_method(name) { val }
        end

        if singleton_class?
          # 定义实例方法
          class_eval do
            remove_possible_method(name)
            define_method(name) do
              # 如果实例变量已经定义,则返回实例变量的值
              if instance_variable_defined? ivar
                instance_variable_get ivar
              else
                # 如果实例变量已经定义,则返回类变量的值
                singleton_class.send name
              end
            end
          end
        end
        val
      end

      # 默认执行,
      if instance_reader
        remove_possible_method name
        define_method(name) do
          if instance_variable_defined?(ivar)
            instance_variable_get ivar
          else
            self.class.public_send name
          end
        end

        remove_possible_method "#{name}?"
        define_method("#{name}?") { !!public_send(name) } if instance_predicate
      end
      # 默认执行,
      if instance_writer
        remove_possible_method "#{name}="
        attr_writer name
      end
    end
  end
end

Resources

相关文章: