How can Ruby's attr_accessor produce class variables or class instance variables instead of instance variables?

后端 未结 2 1567
长情又很酷
长情又很酷 2020-12-07 21:16

If I have a class with an attr_accessor, it defaults to creating an instance variable along with the corresponding getters and setters. But instead of creating

2条回答
  •  长情又很酷
    2020-12-07 21:43

    Like this:

    class TYourClass
      class << self
        attr_accessor :class_instance_variable
      end
    end
    

    You can look at this as opening the metaclass of the class (of which the class itself is an instance) and adding an attribute to it.

    attr_accessor is a method of class Class, it adds two methods to the class, one which reads the instance variable, and other that sets it. Here's a possible implementation:

    class Class
      def my_attr_accessor(name)
        define_method name do
          instance_variable_get "@#{name}"
        end 
        define_method "#{name}=" do |new_val|
          instance_variable_set "@#{name}", new_val
        end
      end
    end
    

    Completely untested class attribute accessor:

    class Class
      def class_attr_accessor(name)
        define_method name do
          class_variable_get "@@#{name}"
        end 
        define_method "#{name}=" do |new_val|
          class_variable_set "@@#{name}", new_val
        end
      end
    end
    

提交回复
热议问题