The ELC Community Blog
A knowledge exchange on Ruby on Rails and Agile Development
write_inheritable_attribute.............. and friends !
by Dylan Stamat on April 05, 2007
Sooooo... many people don't really like using class variables in Ruby, and, they have some valid reasons not to. Color me crazy, but I still use them (very sparingly albeit) once in a while. (i'm a subliminal fan of the @@ syntax !)
For a good primer on the fun that comes with class level attribute inheritance, read this if you haven't already.
ActiveSupport actually contains some friendly helpers for class level attributes.
They can be found in class_inheritable_attributes.rb.
The comment in this file provides a very good briefing of the functionality.
Allows attributes to be shared within an inheritance hierarchy, but where each descendant gets a copy of their parents' attributes, instead of just a pointer to the same. This means that the child can add elements to, for example, an array without those additions being shared with either their parent, siblings, or children, which is unlike the regular class-level attributes that are shared across the entire hierarchy.
Also, the patch description is very well articulated and easy to understand.
Thanks Jeremy !
Go ahead and look in acts_as_taggable, attachment_fu, (and a slew of other plugins that use this happy family of methods) to see them in action.
Nice !
Timeline
- sortable column headers
- DRY validates_inclusion_of with introspection
- HTTP Auth with Restful Authentication
- TuneCore covered on TUAW!
- TabTerm Release
- write_inheritable_attribute.............. and friends !
- Liquid Filter Extensions
- Using and Testing Rails with Multiple Databases
- Securing your Models
- Installing RMagick properly in OSX
- RubyGems 0.91 and the "refresh" error
Comments