Question

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

attr_accessor is a class method defined in class Module

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

Was it helpful?

Solution

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.

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".

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:

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.

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

“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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top