From: "Ball, Donald A Jr (Library)" Date: 2007-05-03T02:09:37+09:00 Subject: Re: simple subclass question Okay, so maybe this turns out to be a not-so-simple subclass question after all. I appreciate all the thoughtful answers and links, I'm pretty close to wrapping my head around it all. I'm still having trouble with class instance variable initialization, and have come up with code sample which should illustrate the point: class Breakfast def self.add_food(*args) @foods ||= [] args.each do |arg| @foods << arg end end def self.foods if superclass.respond_to?(:foods) superclass.foods + @foods else @foods end end add_food :eggs end class RubyBreakfast < Breakfast add_food :grapefruit, :chunky_bacon end class BoringBreakfast < Breakfast add_food :dry_toast, :gruel end Everybody loves breakfast, right? This code works great, except... maybe I don't want Breakfast to have :eggs by default, but if I remove the add_food :eggs from Breakfast, then Breakfast.foods returns nil instead of [] since @foods isn't initialized until add_food is called, and RubyBreakfast.foods throws an exception trying to its delicious foods array to nil. But I'll be damned if I can figure out how to properly initialize @foods in Breakfast. Can someone point me in the proper direction? - donald