红宝石镐书说attr_accessor是类方法 [英] ruby pickaxe book says attr_accessor is class method

查看:73
本文介绍了红宝石镐书说attr_accessor是类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在红宝石镐书中,有一行写着

In the ruby pickaxe book, there is a line that says

attr_accessor是在模块Module中定义的类方法

attr_accessor is a class method defined in class Module

但是attr_accessor不是实例方法吗?我在这里想念东西吗?

But isn't attr_accessor an instance method? Am I missing something here?

推荐答案

是的,我可以找到的所有文档都同意attr_accessor是Module的实例方法,并且我相信它将必须是实例而不是类方法.做它做的事.

Yes, all documentation I can find agrees that attr_accessor is an instance method of Module, and I believe it would have to be an instance rather than class method to do what it does.

我的猜测是,这只是一个错字.作者可能只是想指出,attr_accessor和朋友不是模块语言的一部分,而只是Module类的方法,他们不小心说了类"而不是实例".

My guess is that it's just a typo. The authors were probably just trying to point out that rather than being part of the language proper, attr_accessor and friends are just methods of the Module class, and they accidentally said "class" rather than "instance".

更新:实际上,我的副本(10周年纪念版)中没有您的报价,尽管它是印刷的而不是PDF,所以我不容易搜索它.我能找到的最接近的东西是:

Update: In fact, I can't find your quote in my copy (10th anniversary edition), although it's print rather than PDF, so I can't easily search it. The closest thing I can find is:

类属性声明不是Ruby语法的一部分:它们只是在类Module中定义的方法,它们会自动创建访问器方法.

Class attribute declarations are not part of the Ruby syntax: they are simply methods defined in class Module that create accessor methods automatically.

很有可能您引用的行的编辑版本晚于您的版本.

It's quite possible that the line you quoted was edited out of editions later than yours.

更新2:这是正式的错字.在勘误表中列为第382页:

Update 2: It's officially a typo. Listed as page 382 in the errata:

"attr_accessor是在类Module中定义的类方法,因此在所有模块和类定义中都可用."

"attr_accessor is a class method defined in class Module and so is available in all module and class definitions."

它应该是一个私有实例方法-戴夫·托马斯

It should be a private instance method--Dave Thomas

这篇关于红宝石镐书说attr_accessor是类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆