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

后端 未结 2 1566
长情又很酷
长情又很酷 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:42

    In Rails, (or anywhere you do require 'active_support') you can use cattr_accessor :name to get the true class variable accessors.

    The class instance variables that others have pointed out are usually more useful. The APIdock cattr_accessor page has some helpful discussion clarifying when you would want one not the other, plus the source to the cattr_accessor, cattr_reader and cattr_writer functions.

提交回复
热议问题