This post originated from an RSS feed registered with Ruby Buzz
by Red Handed.
Original Post: Attr for Classes
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
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
end
MailTruck.trucks = [:UPS_TRUCK_9189, :UPS_TRUCK_9192]