Hi -- On Sun, 11 Oct 2009, dreamcat four wrote: > Hi, > I'm having trouble understanding why this isn't available from the Ruby core? > Does core not provide an alternative / satisfactory implementation of this? > > # clia.rb - Class Level Inheritable Attributes > > module ClassLevelInheritableAttributes > def self.included(base) > base.extend(ClassMethods) > end > > module ClassMethods > def inheritable_attributes(*args) > @inheritable_attributes ||= [:inheritable_attributes] > @inheritable_attributes += args > args.each do |arg| > class_eval %( > class << self; attr_accessor :#{arg} end > ) > end > @inheritable_attributes > end > > def inherited(subclass) > @inheritable_attributes.each do |inheritable_attribute| > instance_var = "@#{inheritable_attribute}" > subclass.instance_variable_set(instance_var, > instance_variable_get(instance_var)) > end > end > end > end > > class Polygon > include ClassLevelInheritableAttributes > inheritable_attributes :sides > @sides = 8 > end > > puts Polygon.sides # => 8 > > class Octogon < Polygon; end > > puts Polygon.sides # => 8 > puts Octogon.sides # => 8 > > Ref: http://railstips.org/2006/11/18/class-and-instance-variables-in-ruby > > Its rather difficult to believe we must in each case define / require > our own custom module. Please note - i'm not complaining here. I just > want to know whether the above example is "the best solution in use" > for 1.8... 1.9.... I would probably write it a little differently, maybe like this: module ClassLevelInheritableAttributes def inheritable_attributes(*args) (class << self; self; end).class_eval do attr_accessor *args end @inheritable_attributes ||= [] @inheritable_attributes.concat(args) end def inherited(subclass) @inheritable_attributes.each do |attr| subclass.send("#{attr}=", send(attr)) end super end end class Whatever extend ClassLevelInheritableAttributes end etc. I'm not sure it's a common enough idiom to be worthy of a new language construct, though. I love the fact that you can make what are essentially language-level-like constructs from a few lines of code in Ruby. I think that very, very few of them should be promoted to the actual language. David -- The Ruby training with D. Black, G. Brown, J.McAnally Compleat Jan 22-23, 2010, Tampa, FL Rubyist http://www.thecompleatrubyist.com David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)