Difference between mattr_accessor and cattr_accessor in ActiveSupport?

天涯浪子 提交于 2019-12-04 04:41:20

Module is the superclass of the class Class so if a suitably generic name could be thought of then the methods for defining accessors could be put on Module and it would work for modules and classes. Notice that the follow works:

class Bar
  mattr_accessor :test
end

but

module Foo
  cattr_accessor :test
end

wouldn't work.

Having a c prefix on the methods that should be used inside classes and an m prefix on the methods for use inside modules just helps to make your code a bit clearer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!