hoodwink.d enhanced


Attr for Classes #

by why in inspect

Someone on Ruby-Talk was recently asking about an attr_accessor workalike for Classes. While writing my metaclasses article, I spent a bit of time poking around the goodies in Rails’ active_support directory and came across class_attribute_accessors.rb, which does the business.

After loading the class, use cattr_reader, cattr_writer, and cattr_accessor as you would the instance equivalents.

 class MailTruck
   cattr_accessor :trucks, :customers
 MailTruck.trucks = [:UPS_TRUCK_9189, :UPS_TRUCK_9192]

You can also wield mattr_* methods as well.

said on 25 Apr 2005 at 09:17

This topic has been brought up from time to time since at least 2001. I’m not sure why Matz is so resistant to adding it to core Ruby.

Comments are closed for this entry.