Rails 模块中的 mattr_accessor 是什么?
我在 Rails 文档中找不到这个,但似乎 'mattr_accessor' 是 'attr_accessor' 的 Module 必然结果(getter & setter)在普通的 Ruby 类中。
例如。 在课堂上
class User
attr_accessor :name
def set_fullname
@name = "#{self.first_name} #{self.last_name}"
end
end
例如。 在模块中
module Authentication
mattr_accessor :current_user
def login
@current_user = session[:user_id] || nil
end
end
此辅助方法由 ActiveSupport 提供。
I couldn't really find this in Rails documentation but it seems like 'mattr_accessor' is the Module corollary for 'attr_accessor' (getter & setter) in a normal Ruby class.
Eg. in a class
class User
attr_accessor :name
def set_fullname
@name = "#{self.first_name} #{self.last_name}"
end
end
Eg. in a module
module Authentication
mattr_accessor :current_user
def login
@current_user = session[:user_id] || nil
end
end
This helper method is provided by ActiveSupport.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Rails 使用
mattr_accessor
(模块访问器)和cattr_accessor
(以及 _reader
/_writer
版本)扩展了 Ruby 。 由于 Ruby 的attr_accessor
为实例生成 getter/setter 方法,cattr/mattr_accessor
在类提供 getter/setter 方法或模块级别。 因此:是缩写:
两个版本都允许您访问模块级变量,如下所示:
Rails extends Ruby with both
mattr_accessor
(Module accessor) andcattr_accessor
(as well as _reader
/_writer
versions). As Ruby'sattr_accessor
generates getter/setter methods for instances,cattr/mattr_accessor
provide getter/setter methods at the class or module level. Thus:is short for:
Both versions allow you to access the module-level variables like so:
这里是 <代码>cattr_accessor
和
这是
mattr_accessor
的源代码正如您所看到的,它们几乎相同。
至于为什么会有两个不同的版本? 有时您想在模块中编写
cattr_accessor
,以便可以将其用于配置信息就像 Avdi 提到的。但是,
cattr_accessor
无法在模块中工作,因此他们或多或少地复制了代码以也可以在模块中工作。此外,有时您可能想在模块中编写类方法,这样每当任何类包含该模块时,它都会获取该类方法以及所有实例方法。
mattr_accessor
也可以让您执行此操作。然而,在第二种情况下,它的行为非常奇怪。 观察以下代码,特别注意
@@mattr_in_module
位Here's the source for
cattr_accessor
And
Here's the source for
mattr_accessor
As you can see, they're pretty much identical.
As to why there are two different versions? Sometimes you want to write
cattr_accessor
in a module, so you can use it for configuration info like Avdi mentions.However,
cattr_accessor
doesn't work in a module, so they more or less copied the code over to work for modules also.Additionally, sometimes you might want to write a class method in a module, such that whenever any class includes the module, it gets that class method as well as all the instance methods.
mattr_accessor
also lets you do this.However, in the second scenario, it's behaviour is pretty strange. Observe the following code, particularly note the
@@mattr_in_module
bits