gpt4 book ai didi

ruby 镐书说 attr_accessor 是类方法

转载 作者:太空宇宙 更新时间:2023-11-03 17:56:56 25 4
gpt4 key购买 nike

在 ruby 镐书中,有一行是这样说的

attr_accessor is a class method defined in class Module

但是 attr_accessor 不是实例方法吗?我在这里遗漏了什么吗?

最佳答案

是的,我能找到的所有文档都同意 attr_accessor 是 Module 的一个实例方法,我相信它必须是一个实例而不是类方法才能完成它的工作。

我猜这只是一个打字错误。作者可能只是想指出,attr_accessor 和 friend 只是 Module 类的方法,而不是语言本身的一部分,他们不小心说了“类”而不是“实例”。

更新:事实上,我无法在我的副本(10 周年纪念版)中找到您的报价,尽管它是打印品而不是 PDF,所以我无法轻松搜索它。我能找到的最接近的东西是:

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

很有可能你引用的那行是从比你的版本晚的版本中编辑出来的。

更新 2:这是正式的拼写错误。在 the errata 中列为第 382 页:

“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

关于 ruby 镐书说 attr_accessor 是类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11731698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com